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 
29 #include "config.h"
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 
42 #if FF_API_REORDERED_OPAQUE
43  int64_t reordered_opaque;
44 #endif
47 
48  int done; // If true, we have assembled the bitstream already
50 
52 {
54  if (!ret) {
55  LibWebPAnimContext *s = avctx->priv_data;
56  WebPAnimEncoderOptions enc_options = { { 0 } };
57  WebPAnimEncoderOptionsInit(&enc_options);
58  enc_options.verbose = av_log_get_level() >= AV_LOG_VERBOSE;
59  // TODO(urvang): Expose some options on command-line perhaps.
60  s->enc = WebPAnimEncoderNew(avctx->width, avctx->height, &enc_options);
61  if (!s->enc)
62  return AVERROR(EINVAL);
63  s->first_frame_pts = AV_NOPTS_VALUE;
64  s->done = 0;
65  }
66  return ret;
67 }
68 
70  const AVFrame *frame, int *got_packet) {
71  LibWebPAnimContext *s = avctx->priv_data;
72  int ret;
73 
74  if (!frame) {
75  if (s->done) { // Second flush: return empty package to denote finish.
76  *got_packet = 0;
77  return 0;
78  } else { // First flush: assemble bitstream and return it.
79  WebPData assembled_data = { 0 };
80  ret = WebPAnimEncoderAssemble(s->enc, &assembled_data);
81  if (ret) {
82  ret = ff_get_encode_buffer(avctx, pkt, assembled_data.size, 0);
83  if (ret < 0) {
84  WebPDataClear(&assembled_data);
85  return ret;
86  }
87  memcpy(pkt->data, assembled_data.bytes, assembled_data.size);
88  WebPDataClear(&assembled_data);
89  s->done = 1;
90  pkt->pts = s->first_frame_pts;
91 
92  if (pkt->pts != AV_NOPTS_VALUE && s->end_pts > pkt->pts)
93  pkt->duration = s->end_pts - pkt->pts;
94 
95 #if FF_API_REORDERED_OPAQUE
97  avctx->reordered_opaque = s->reordered_opaque;
99 #endif
100  if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
101  pkt->opaque = s->first_frame_opaque;
102  pkt->opaque_ref = s->first_frame_opaque_ref;
103  s->first_frame_opaque_ref = NULL;
104  }
105 
106  *got_packet = 1;
107  return 0;
108  } else {
109  WebPDataClear(&assembled_data);
111  "WebPAnimEncoderAssemble() failed with error: %d\n",
112  VP8_ENC_ERROR_OUT_OF_MEMORY);
113  return AVERROR(ENOMEM);
114  }
115  }
116  } else {
117  int timestamp_ms;
118  WebPPicture *pic = NULL;
119  AVFrame *alt_frame = NULL;
120  ret = ff_libwebp_get_frame(avctx, &s->cc, frame, &alt_frame, &pic);
121  if (ret < 0)
122  goto end;
123 
124  timestamp_ms =
125  avctx->time_base.num * frame->pts * 1000 / avctx->time_base.den;
126  ret = WebPAnimEncoderAdd(s->enc, pic, timestamp_ms, &s->cc.config);
127  if (!ret) {
128  av_log(avctx, AV_LOG_ERROR,
129  "Encoding WebP frame failed with error: %d\n",
130  pic->error_code);
131  ret = ff_libwebp_error_to_averror(pic->error_code);
132  goto end;
133  }
134 
135  if (!avctx->frame_num) {
136  s->first_frame_pts = frame->pts;
137 #if FF_API_REORDERED_OPAQUE
139  s->reordered_opaque = frame->reordered_opaque;
141 #endif
142 
143  if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
144  s->first_frame_opaque = frame->opaque;
145  ret = av_buffer_replace(&s->first_frame_opaque_ref, frame->opaque_ref);
146  if (ret < 0)
147  goto end;
148  }
149  }
150 
151  if (frame->pts != AV_NOPTS_VALUE)
152  s->end_pts = frame->pts + frame->duration;
153 
154  ret = 0;
155  *got_packet = 0;
156 
157 end:
158  WebPPictureFree(pic);
159  av_freep(&pic);
160  av_frame_free(&alt_frame);
161  return ret;
162  }
163 }
164 
166 {
167  LibWebPAnimContext *s = avctx->priv_data;
168  av_frame_free(&s->cc.ref);
169  WebPAnimEncoderDelete(s->enc);
170 
171  av_buffer_unref(&s->first_frame_opaque_ref);
172 
173  return 0;
174 }
175 
177  .p.name = "libwebp_anim",
178  CODEC_LONG_NAME("libwebp WebP image"),
179  .p.type = AVMEDIA_TYPE_VIDEO,
180  .p.id = AV_CODEC_ID_WEBP,
181  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
183  .p.pix_fmts = ff_libwebpenc_pix_fmts,
184  .p.priv_class = &ff_libwebpenc_class,
185  .p.wrapper_name = "libwebp",
186  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
187  .priv_data_size = sizeof(LibWebPAnimContext),
191  .close = libwebp_anim_encode_close,
192 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
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:67
ff_libwebp_anim_encoder
const FFCodec ff_libwebp_anim_encoder
Definition: libwebpenc_animencoder.c:176
defaults
static const FFCodecDefault defaults[]
Definition: amfenc_av1.c:454
ff_libwebp_encode_init_common
av_cold int ff_libwebp_encode_init_common(AVCodecContext *avctx)
Definition: libwebpenc_common.c:81
AVFrame::duration
int64_t duration
Duration of the frame, in the same units as pts.
Definition: frame.h:807
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
AVFrame::opaque
void * opaque
Frame owner's private data.
Definition: frame.h:501
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:452
AVPacket::data
uint8_t * data
Definition: packet.h:491
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:34
FFCodec
Definition: codec_internal.h:127
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:509
AVFrame::opaque_ref
AVBufferRef * opaque_ref
Frame owner's private data.
Definition: frame.h:768
AV_CODEC_FLAG_COPY_OPAQUE
#define AV_CODEC_FLAG_COPY_OPAQUE
Definition: avcodec.h:295
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
LibWebPAnimContext::done
int done
Definition: libwebpenc_animencoder.c:48
AVPacket::opaque_ref
AVBufferRef * opaque_ref
AVBufferRef for free use by the API user.
Definition: packet.h:527
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:521
libwebp_anim_encode_init
static av_cold int libwebp_anim_encode_init(AVCodecContext *avctx)
Definition: libwebpenc_animencoder.c:51
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:315
AVRational::num
int num
Numerator.
Definition: rational.h:59
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AVFrame::reordered_opaque
attribute_deprecated int64_t reordered_opaque
reordered opaque 64 bits (generally an integer or a double precision float PTS but can be anything).
Definition: frame.h:561
s
#define s(width, name)
Definition: cbs_vp9.c:198
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
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:165
AVPacket::opaque
void * opaque
for some private data of the user
Definition: packet.h:516
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
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:437
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:30
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:563
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:492
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:484
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:224
AVCodecContext::height
int height
Definition: avcodec.h:621
LibWebPAnimContext::first_frame_opaque
void * first_frame_opaque
Definition: libwebpenc_animencoder.c:45
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2118
ret
ret
Definition: filter_design.txt:187
LibWebPAnimContext::first_frame_opaque_ref
AVBufferRef * first_frame_opaque_ref
Definition: libwebpenc_animencoder.c:46
AVCodecContext
main external API structure.
Definition: avcodec.h:441
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:105
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:124
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
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVPacket
This structure stores compressed data.
Definition: packet.h:468
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:468
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:61
libwebpenc_common.h
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
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:69
ff_libwebpenc_class
const AVClass ff_libwebpenc_class
Definition: libwebpenc_common.c:54