FFmpeg
Loading...
Searching...
No Matches
libwebpenc_animencoder.c
Go to the documentation of this file.
1/*
2 * WebP encoding support via libwebp
3 * Copyright (c) 2015 Urvang Joshi
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * WebP encoder using libwebp (WebPAnimEncoder API)
25 */
26
27#include "libavutil/buffer.h"
28#include "libavutil/mem.h"
29
30#include "codec_internal.h"
31#include "encode.h"
32#include "libwebpenc_common.h"
33
34#include <webp/mux.h>
35
36typedef struct LibWebPAnimContext {
38 WebPAnimEncoder *enc; // the main AnimEncoder object
39 int64_t first_frame_pts; // pts of the first encoded frame.
40 int64_t end_pts; // pts + duration of the last frame
41
44
45 int done; // If true, we have assembled the bitstream already
47
49{
50 int ret = ff_libwebp_encode_init_common(avctx);
51 if (!ret) {
53 WebPAnimEncoderOptions enc_options = { { 0 } };
54 WebPAnimEncoderOptionsInit(&enc_options);
55 enc_options.verbose = av_log_get_level() >= AV_LOG_VERBOSE;
56 // TODO(urvang): Expose some options on command-line perhaps.
57 s->enc = WebPAnimEncoderNew(avctx->width, avctx->height, &enc_options);
58 if (!s->enc)
59 return AVERROR(EINVAL);
60 s->first_frame_pts = AV_NOPTS_VALUE;
61 s->done = 0;
62 }
63 return ret;
64}
65
67 const AVFrame *frame, int *got_packet) {
69 int ret;
70
71 if (!frame) {
72 if (s->done) { // Second flush: return empty package to denote finish.
73 *got_packet = 0;
74 return 0;
75 } else { // First flush: assemble bitstream and return it.
76 WebPData assembled_data = { 0 };
77 ret = WebPAnimEncoderAssemble(s->enc, &assembled_data);
78 if (ret) {
79 ret = ff_get_encode_buffer(avctx, pkt, assembled_data.size, 0);
80 if (ret < 0) {
81 WebPDataClear(&assembled_data);
82 return ret;
83 }
84 memcpy(pkt->data, assembled_data.bytes, assembled_data.size);
85 WebPDataClear(&assembled_data);
86 s->done = 1;
87 pkt->pts = s->first_frame_pts;
88
89 if (pkt->pts != AV_NOPTS_VALUE && s->end_pts > pkt->pts)
90 pkt->duration = s->end_pts - pkt->pts;
91
92 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
93 pkt->opaque = s->first_frame_opaque;
94 pkt->opaque_ref = s->first_frame_opaque_ref;
95 s->first_frame_opaque_ref = NULL;
96 }
97
98 *got_packet = 1;
99 return 0;
100 } else {
101 WebPDataClear(&assembled_data);
103 "WebPAnimEncoderAssemble() failed with error: %d\n",
104 VP8_ENC_ERROR_OUT_OF_MEMORY);
105 return AVERROR(ENOMEM);
106 }
107 }
108 } else {
109 int timestamp_ms;
110 WebPPicture *pic = NULL;
111 AVFrame *alt_frame = NULL;
112 ret = ff_libwebp_get_frame(avctx, &s->cc, frame, &alt_frame, &pic);
113 if (ret < 0)
114 goto end;
115
116 timestamp_ms =
117 avctx->time_base.num * frame->pts * 1000 / avctx->time_base.den;
118 ret = WebPAnimEncoderAdd(s->enc, pic, timestamp_ms, &s->cc.config);
119 if (!ret) {
120 av_log(avctx, AV_LOG_ERROR,
121 "Encoding WebP frame failed with error: %d\n",
122 pic->error_code);
123 ret = ff_libwebp_error_to_averror(pic->error_code);
124 goto end;
125 }
126
127 if (!avctx->frame_num) {
128 s->first_frame_pts = frame->pts;
129
130 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
131 s->first_frame_opaque = frame->opaque;
132 ret = av_buffer_replace(&s->first_frame_opaque_ref, frame->opaque_ref);
133 if (ret < 0)
134 goto end;
135 }
136 }
137
138 if (frame->pts != AV_NOPTS_VALUE)
139 s->end_pts = frame->pts + frame->duration;
140
141 ret = 0;
142 *got_packet = 0;
143
144end:
145 WebPPictureFree(pic);
146 av_freep(&pic);
147 av_frame_free(&alt_frame);
148 return ret;
149 }
150}
151
153{
155 av_frame_free(&s->cc.ref);
156 WebPAnimEncoderDelete(s->enc);
157
158 av_buffer_unref(&s->first_frame_opaque_ref);
159
160 return 0;
161}
162
164 .p.name = "libwebp_anim",
165 CODEC_LONG_NAME("libwebp WebP image"),
166 .p.type = AVMEDIA_TYPE_VIDEO,
167 .p.id = AV_CODEC_ID_WEBP,
168 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
171 .color_ranges = AVCOL_RANGE_MPEG,
172 .p.priv_class = &ff_libwebpenc_class,
173 .p.wrapper_name = "libwebp",
174 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
175 .priv_data_size = sizeof(LibWebPAnimContext),
180};
const FFCodec ff_libwebp_anim_encoder
static const FFCodecDefault defaults[]
Definition amfenc_av1.c:723
refcounted data buffer API
#define s(width, name)
Definition cbs_vp9.c:198
#define CODEC_PIXFMTS_ARRAY(array)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_FLAG_COPY_OPAQUE
Definition avcodec.h:279
@ AV_CODEC_ID_WEBP
Definition codec_id.h:221
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition buffer.c:233
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_log_get_level(void)
Get the current log level.
Definition log.c:471
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define av_cold
Definition attributes.h:117
static int libwebp_anim_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
static int libwebp_anim_encode_close(AVCodecContext *avctx)
static av_cold int libwebp_anim_encode_init(AVCodecContext *avctx)
int ff_libwebp_get_frame(AVCodecContext *avctx, LibWebPContextCommon *s, const AVFrame *frame, AVFrame **alt_frame_ptr, WebPPicture **pic_ptr)
const AVClass ff_libwebpenc_class
enum AVPixelFormat ff_libwebpenc_pix_fmts[]
av_cold int ff_libwebp_encode_init_common(AVCodecContext *avctx)
int ff_libwebp_error_to_averror(int err)
const FFCodecDefault ff_libwebp_defaults[]
WebP encoder using libwebp: common structs and methods.
Memory handling functions.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
int width
picture width / height.
Definition avcodec.h:604
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1883
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avcodec.h:547
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
AVBufferRef * first_frame_opaque_ref
LibWebPContextCommon cc
#define av_freep(p)
#define av_log(a,...)