FFmpeg
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 
36 typedef 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 {
51  if (!ret) {
52  LibWebPAnimContext *s = avctx->priv_data;
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) {
68  LibWebPAnimContext *s = avctx->priv_data;
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 
144 end:
145  WebPPictureFree(pic);
146  av_freep(&pic);
147  av_frame_free(&alt_frame);
148  return ret;
149  }
150 }
151 
153 {
154  LibWebPAnimContext *s = avctx->priv_data;
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 |
170  .p.pix_fmts = ff_libwebpenc_pix_fmts,
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),
179  .close = libwebp_anim_encode_close,
180 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_libwebp_error_to_averror
int ff_libwebp_error_to_averror(int err)
Definition: libwebpenc_common.c:68
ff_libwebp_anim_encoder
const FFCodec ff_libwebp_anim_encoder
Definition: libwebpenc_animencoder.c:163
defaults
static const FFCodecDefault defaults[]
Definition: amfenc_av1.c:603
ff_libwebp_encode_init_common
av_cold int ff_libwebp_encode_init_common(AVCodecContext *avctx)
Definition: libwebpenc_common.c:82
int64_t
long long int64_t
Definition: coverity.c:34
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
encode.h
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:35
FFCodec
Definition: codec_internal.h:127
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
AV_CODEC_FLAG_COPY_OPAQUE
#define AV_CODEC_FLAG_COPY_OPAQUE
Definition: avcodec.h:299
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
LibWebPAnimContext::done
int done
Definition: libwebpenc_animencoder.c:45
AVPacket::opaque_ref
AVBufferRef * opaque_ref
AVBufferRef for free use by the API user.
Definition: packet.h:575
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:508
libwebp_anim_encode_init
static av_cold int libwebp_anim_encode_init(AVCodecContext *avctx)
Definition: libwebpenc_animencoder.c:48
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:320
AVRational::num
int num
Numerator.
Definition: rational.h:59
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#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:159
libwebp_anim_encode_close
static int libwebp_anim_encode_close(AVCodecContext *avctx)
Definition: libwebpenc_animencoder.c:152
AVPacket::opaque
void * opaque
for some private data of the user
Definition: packet.h:564
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
LibWebPAnimContext::end_pts
int64_t end_pts
Definition: libwebpenc_animencoder.c:40
av_log_get_level
int av_log_get_level(void)
Get the current log level.
Definition: log.c:442
NULL
#define NULL
Definition: coverity.c:32
av_buffer_unref
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
ff_libwebp_defaults
const FFCodecDefault ff_libwebp_defaults[]
Definition: libwebpenc_common.c:31
LibWebPAnimContext
Definition: libwebpenc_animencoder.c:36
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:550
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
codec_internal.h
LibWebPAnimContext::enc
WebPAnimEncoder * enc
Definition: libwebpenc_animencoder.c:38
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
buffer.h
LibWebPContextCommon
Definition: libwebpenc_common.h:39
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
av_buffer_replace
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition: buffer.c:233
AV_CODEC_ID_WEBP
@ AV_CODEC_ID_WEBP
Definition: codec_id.h:226
AVCodecContext::height
int height
Definition: avcodec.h:624
LibWebPAnimContext::first_frame_opaque
void * first_frame_opaque
Definition: libwebpenc_animencoder.c:42
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:700
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2041
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
LibWebPAnimContext::first_frame_opaque_ref
AVBufferRef * first_frame_opaque_ref
Definition: libwebpenc_animencoder.c:43
AVCodecContext
main external API structure.
Definition: avcodec.h:451
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:106
AVRational::den
int den
Denominator.
Definition: rational.h:60
ff_libwebp_get_frame
int ff_libwebp_get_frame(AVCodecContext *avctx, LibWebPContextCommon *s, const AVFrame *frame, AVFrame **alt_frame_ptr, WebPPicture **pic_ptr)
Definition: libwebpenc_common.c:125
AV_CODEC_CAP_DELAY
#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:76
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
ff_libwebpenc_pix_fmts
enum AVPixelFormat ff_libwebpenc_pix_fmts[]
Definition: libwebpenc_common.c:62
libwebpenc_common.h
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
LibWebPAnimContext::cc
LibWebPContextCommon cc
Definition: libwebpenc_animencoder.c:37
LibWebPAnimContext::first_frame_pts
int64_t first_frame_pts
Definition: libwebpenc_animencoder.c:39
libwebp_anim_encode_frame
static int libwebp_anim_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: libwebpenc_animencoder.c:66
ff_libwebpenc_class
const AVClass ff_libwebpenc_class
Definition: libwebpenc_common.c:55