]> git.sesse.net Git - ffmpeg/blob - libavformat/img2enc.c
mpegvideo_enc: export vbv_delay in side data
[ffmpeg] / libavformat / img2enc.c
1 /*
2  * Image format
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  * Copyright (c) 2004 Michael Niedermayer
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/log.h"
26 #include "avformat.h"
27 #include "avio_internal.h"
28 #include "internal.h"
29 #include "libavutil/opt.h"
30
31 typedef struct VideoMuxData {
32     const AVClass *class;  /**< Class for private options. */
33     int img_number;
34     int is_pipe;
35     char path[1024];
36     char tmp[1024];
37     int update;
38 } VideoMuxData;
39
40 static int write_header(AVFormatContext *s)
41 {
42     VideoMuxData *img = s->priv_data;
43
44     av_strlcpy(img->path, s->filename, sizeof(img->path));
45     snprintf(img->tmp, sizeof(img->tmp), "%s.tmp", s->filename);
46
47     /* find format */
48     if (s->oformat->flags & AVFMT_NOFILE)
49         img->is_pipe = 0;
50     else
51         img->is_pipe = 1;
52
53     return 0;
54 }
55
56 static int write_packet(AVFormatContext *s, AVPacket *pkt)
57 {
58     VideoMuxData *img = s->priv_data;
59     AVIOContext *pb[3];
60     char filename[1024];
61     AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
62     int i;
63
64     if (!img->is_pipe) {
65         if (img->update) {
66             av_strlcpy(filename, img->path, sizeof(filename));
67         } else if (av_get_frame_filename(filename, sizeof(filename), img->path, img->img_number) < 0 &&
68                    img->img_number > 1) {
69             av_log(s, AV_LOG_ERROR,
70                    "Could not get frame filename number %d from pattern '%s'\n",
71                    img->img_number, img->path);
72             return AVERROR(EIO);
73         }
74         for (i = 0; i < 3; i++) {
75             if (avio_open2(&pb[i], img->tmp, AVIO_FLAG_WRITE,
76                            &s->interrupt_callback, NULL) < 0) {
77                 av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", img->tmp);
78                 return AVERROR(EIO);
79             }
80
81             if (codec->codec_id != AV_CODEC_ID_RAWVIDEO)
82                 break;
83             filename[strlen(filename) - 1] = 'U' + i;
84         }
85     } else {
86         pb[0] = s->pb;
87     }
88
89     if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
90         int ysize = codec->width * codec->height;
91         avio_write(pb[0], pkt->data, ysize);
92         avio_write(pb[1], pkt->data + ysize,                           (pkt->size - ysize) / 2);
93         avio_write(pb[2], pkt->data + ysize + (pkt->size - ysize) / 2, (pkt->size - ysize) / 2);
94         avio_close(pb[1]);
95         avio_close(pb[2]);
96     } else {
97         if (ff_guess_image2_codec(s->filename) == AV_CODEC_ID_JPEG2000) {
98             AVStream *st = s->streams[0];
99             if (st->codec->extradata_size > 8 &&
100                 AV_RL32(st->codec->extradata + 4) == MKTAG('j', 'p', '2', 'h')) {
101                 if (pkt->size < 8 ||
102                     AV_RL32(pkt->data + 4) != MKTAG('j', 'p', '2', 'c'))
103                     goto error;
104                 avio_wb32(pb[0], 12);
105                 ffio_wfourcc(pb[0], "jP  ");
106                 avio_wb32(pb[0], 0x0D0A870A); // signature
107                 avio_wb32(pb[0], 20);
108                 ffio_wfourcc(pb[0], "ftyp");
109                 ffio_wfourcc(pb[0], "jp2 ");
110                 avio_wb32(pb[0], 0);
111                 ffio_wfourcc(pb[0], "jp2 ");
112                 avio_write(pb[0], st->codec->extradata, st->codec->extradata_size);
113             } else if (pkt->size < 8 ||
114                        (!st->codec->extradata_size &&
115                         AV_RL32(pkt->data + 4) != MKTAG('j', 'P', ' ', ' '))) { // signature
116 error:
117                 av_log(s, AV_LOG_ERROR, "malformed JPEG 2000 codestream\n");
118                 return -1;
119             }
120         }
121         avio_write(pb[0], pkt->data, pkt->size);
122     }
123     avio_flush(pb[0]);
124     if (!img->is_pipe) {
125         avio_close(pb[0]);
126         ff_rename(img->tmp, filename);
127     }
128
129     img->img_number++;
130     return 0;
131 }
132
133 #define OFFSET(x) offsetof(VideoMuxData, x)
134 #define ENC AV_OPT_FLAG_ENCODING_PARAM
135 static const AVOption muxoptions[] = {
136     { "start_number", "first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, ENC },
137     { "update",       "continuously overwrite one file", OFFSET(update),  AV_OPT_TYPE_INT, { .i64 = 0 }, 0,       1, ENC },
138     { NULL },
139 };
140
141 #if CONFIG_IMAGE2_MUXER
142 static const AVClass img2mux_class = {
143     .class_name = "image2 muxer",
144     .item_name  = av_default_item_name,
145     .option     = muxoptions,
146     .version    = LIBAVUTIL_VERSION_INT,
147 };
148
149 AVOutputFormat ff_image2_muxer = {
150     .name           = "image2",
151     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
152     .extensions     = "bmp,dpx,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
153                       "ppm,sgi,tga,tif,tiff,jp2,xwd,sun,ras,rs,im1,im8,im24,"
154                       "sunras,webp,xbm,j2c,pix",
155     .priv_data_size = sizeof(VideoMuxData),
156     .video_codec    = AV_CODEC_ID_MJPEG,
157     .write_header   = write_header,
158     .write_packet   = write_packet,
159     .flags          = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
160     .priv_class     = &img2mux_class,
161 };
162 #endif
163 #if CONFIG_IMAGE2PIPE_MUXER
164 AVOutputFormat ff_image2pipe_muxer = {
165     .name           = "image2pipe",
166     .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
167     .priv_data_size = sizeof(VideoMuxData),
168     .video_codec    = AV_CODEC_ID_MJPEG,
169     .write_header   = write_header,
170     .write_packet   = write_packet,
171     .flags          = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
172 };
173 #endif