FFmpeg: libavformat/webpenc.c Source File

FFmpeg
webpenc.c
Go to the documentation of this file.
1 /*
2  * webp muxer
3  * Copyright (c) 2014 Michael Niedermayer
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 #include "libavutil/intreadwrite.h"
23 #include "libavutil/opt.h"
24 #include "avformat.h"
25 #include "internal.h"
26 
27  typedef struct WebpContext{
28   AVClass *class;
29   int frame_count;
30   AVPacket last_pkt;
31   int loop;
32   int wrote_webp_header;
33   int using_webp_anim_encoder;
34 } WebpContext;
35 
36  static int webp_write_header(AVFormatContext *s)
37 {
38  AVStream *st;
39 
40  if (s->nb_streams != 1) {
41  av_log(s, AV_LOG_ERROR, "Only exactly 1 stream is supported\n");
42  return AVERROR(EINVAL);
43  }
44  st = s->streams[0];
45  if (st->codec->codec_id != AV_CODEC_ID_WEBP) {
46  av_log(s, AV_LOG_ERROR, "Only WebP is supported\n");
47  return AVERROR(EINVAL);
48  }
49  avpriv_set_pts_info(st, 24, 1, 1000);
50 
51  return 0;
52 }
53 
54  static int is_animated_webp_packet(AVPacket *pkt)
55 {
56  if (pkt->size) {
57  int skip = 0;
58  unsigned flags = 0;
59 
60  if (pkt->size < 4)
61  return 0;
62  if (AV_RL32(pkt->data) == AV_RL32("RIFF"))
63  skip = 12;
64 
65  if (pkt->size < skip + 4)
66  return 0;
67  if (AV_RL32(pkt->data + skip) == AV_RL32("VP8X")) {
68  flags |= pkt->data[skip + 4 + 4];
69  }
70 
71  if (flags & 2) // ANIMATION_FLAG is on
72  return 1;
73  }
74  return 0;
75 }
76 
77  static int flush(AVFormatContext *s, int trailer, int64_t pts)
78 {
79  WebpContext *w = s->priv_data;
80  AVStream *st = s->streams[0];
81 
82  if (w->last_pkt.size) {
83  int skip = 0;
84  unsigned flags = 0;
85  int vp8x = 0;
86 
87  if (w->last_pkt.size < 4)
88  return 0;
89  if (AV_RL32(w->last_pkt.data) == AV_RL32("RIFF"))
90  skip = 12;
91 
92  if (w->last_pkt.size < skip + 4)
93  return 0; // Safe to do this as a valid WebP bitstream is >=30 bytes.
94  if (AV_RL32(w->last_pkt.data + skip) == AV_RL32("VP8X")) {
95  flags |= w->last_pkt.data[skip + 4 + 4];
96  vp8x = 1;
97  skip += AV_RL32(w->last_pkt.data + skip + 4) + 8;
98  }
99 
100  if (!w->wrote_webp_header) {
101  avio_write(s->pb, "RIFF0円0円0円0円WEBP", 12);
102  w->wrote_webp_header = 1;
103  if (w->frame_count > 1) // first non-empty packet
104  w->frame_count = 1; // so we don't count previous empty packets.
105  }
106 
107  if (w->frame_count == 1) {
108  if (!trailer) {
109  vp8x = 1;
110  flags |= 2 + 16;
111  }
112 
113  if (vp8x) {
114  avio_write(s->pb, "VP8X", 4);
115  avio_wl32(s->pb, 10);
116  avio_w8(s->pb, flags);
117  avio_wl24(s->pb, 0);
118  avio_wl24(s->pb, st->codec->width - 1);
119  avio_wl24(s->pb, st->codec->height - 1);
120  }
121  if (!trailer) {
122  avio_write(s->pb, "ANIM", 4);
123  avio_wl32(s->pb, 6);
124  avio_wl32(s->pb, 0xFFFFFFFF);
125  avio_wl16(s->pb, w->loop);
126  }
127  }
128 
129  if (w->frame_count > trailer) {
130  avio_write(s->pb, "ANMF", 4);
131  avio_wl32(s->pb, 16 + w->last_pkt.size - skip);
132  avio_wl24(s->pb, 0);
133  avio_wl24(s->pb, 0);
134  avio_wl24(s->pb, st->codec->width - 1);
135  avio_wl24(s->pb, st->codec->height - 1);
136  if (w->last_pkt.pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE) {
137  avio_wl24(s->pb, pts - w->last_pkt.pts);
138  } else
139  avio_wl24(s->pb, w->last_pkt.duration);
140  avio_w8(s->pb, 0);
141  }
142  avio_write(s->pb, w->last_pkt.data + skip, w->last_pkt.size - skip);
143  av_packet_unref(&w->last_pkt);
144  }
145 
146  return 0;
147 }
148 
149  static int webp_write_packet(AVFormatContext *s, AVPacket *pkt)
150 {
151  WebpContext *w = s->priv_data;
152  w->using_webp_anim_encoder |= is_animated_webp_packet(pkt);
153 
154  if (w->using_webp_anim_encoder) {
155  avio_write(s->pb, pkt->data, pkt->size);
156  w->wrote_webp_header = 1; // for good measure
157  } else {
158  int ret;
159  if ((ret = flush(s, 0, pkt->pts)) < 0)
160  return ret;
161  av_copy_packet(&w->last_pkt, pkt);
162  }
163  ++w->frame_count;
164 
165  return 0;
166 }
167 
168  static int webp_write_trailer(AVFormatContext *s)
169 {
170  unsigned filesize;
171  WebpContext *w = s->priv_data;
172 
173  if (w->using_webp_anim_encoder) {
174  if ((w->frame_count > 1) && w->loop) { // Write loop count.
175  avio_seek(s->pb, 42, SEEK_SET);
176  avio_wl16(s->pb, w->loop);
177  }
178  } else {
179  int ret;
180  if ((ret = flush(s, 1, AV_NOPTS_VALUE)) < 0)
181  return ret;
182 
183  filesize = avio_tell(s->pb);
184  avio_seek(s->pb, 4, SEEK_SET);
185  avio_wl32(s->pb, filesize - 8);
186  // Note: without the following, avio only writes 8 bytes to the file.
187  avio_seek(s->pb, filesize, SEEK_SET);
188  }
189 
190  return 0;
191 }
192 
193  #define OFFSET(x) offsetof(WebpContext, x)
194  #define ENC AV_OPT_FLAG_ENCODING_PARAM
195  static const AVOption options[] = {
196  { "loop", "Number of times to loop the output: 0 - infinite loop", OFFSET(loop),
197  AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 65535, ENC },
198  { NULL },
199 };
200 
201  static const AVClass webp_muxer_class = {
202  .class_name = "WebP muxer",
203  .item_name = av_default_item_name,
204  .version = LIBAVUTIL_VERSION_INT,
205  .option = options,
206 };
207  AVOutputFormat ff_webp_muxer = {
208  .name = "webp",
209  .long_name = NULL_IF_CONFIG_SMALL("WebP"),
210  .extensions = "webp",
211  .priv_data_size = sizeof(WebpContext),
212  .video_codec = AV_CODEC_ID_WEBP,
213  .write_header = webp_write_header,
214  .write_packet = webp_write_packet,
215  .write_trailer = webp_write_trailer,
216  .priv_class = &webp_muxer_class,
217  .flags = AVFMT_VARIABLE_FPS,
218 };
NULL
#define NULL
Definition: coverity.c:32
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:424
s
const char * s
Definition: avisynth_c.h:631
AVOption
AVOption.
Definition: opt.h:245
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
WebpContext::last_pkt
AVPacket last_pkt
Definition: webpenc.c:30
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4149
AVPacket::size
int size
Definition: avcodec.h:1468
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:208
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
WebpContext::loop
int loop
Definition: webpenc.c:31
AVFormatContext
Format I/O context.
Definition: avformat.h:1314
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:324
opt.h
AVOptions.
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1485
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
AVPacket::data
uint8_t * data
Definition: avcodec.h:1467
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:442
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:182
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
options
static const AVOption options[]
Definition: webpenc.c:195
av_default_item_name
av_default_item_name
Definition: libopenh264enc.c:68
AVERROR
#define AVERROR(e)
Definition: error.h:43
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
WebpContext::using_webp_anim_encoder
int using_webp_anim_encoder
Definition: webpenc.c:33
AVStream::codec
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1370
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:1711
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
AVOutputFormat::name
const char * name
Definition: avformat.h:523
webp_write_header
static int webp_write_header(AVFormatContext *s)
Definition: webpenc.c:36
WebpContext::wrote_webp_header
int wrote_webp_header
Definition: webpenc.c:32
is_animated_webp_packet
static int is_animated_webp_packet(AVPacket *pkt)
Definition: webpenc.c:54
AVCodecContext::height
int height
Definition: avcodec.h:1711
WebpContext::frame_count
int frame_count
Definition: webpenc.c:29
webp_muxer_class
static const AVClass webp_muxer_class
Definition: webpenc.c:201
AVStream
Stream structure.
Definition: avformat.h:877
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:1549
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
loop
static int loop
Definition: ffplay.c:339
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:160
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:545
av_copy_packet
int av_copy_packet(AVPacket *dst, const AVPacket *src)
Copy packet, including contents.
Definition: avpacket.c:246
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
avio_wl24
void avio_wl24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:436
flush
static int flush(AVFormatContext *s, int trailer, int64_t pts)
Definition: webpenc.c:77
pts
static int64_t pts
Global timestamp for the audio frames.
Definition: transcode_aac.c:547
flags
static int flags
Definition: cpu.c:47
webp_write_packet
static int webp_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: webpenc.c:149
ff_webp_muxer
AVOutputFormat ff_webp_muxer
Definition: webpenc.c:207
avformat.h
Main libavformat public API header.
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:488
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1342
OFFSET
#define OFFSET(x)
Definition: webpenc.c:193
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:497
webp_write_trailer
static int webp_write_trailer(AVFormatContext *s)
Definition: webpenc.c:168
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1444
write_packet
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1460
ENC
#define ENC
Definition: webpenc.c:194
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240

Generated on Mon Feb 15 2016 15:20:47 for FFmpeg by   doxygen 1.8.6

AltStyle によって変換されたページ (->オリジナル) /