]> git.sesse.net Git - ffmpeg/blob - libavformat/img2enc.c
7f0ff19ba37d0acc69861542332936330701429b
[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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/avassert.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/log.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/time_internal.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "internal.h"
33 #include "img2.h"
34
35 typedef struct VideoMuxData {
36     const AVClass *class;  /**< Class for private options. */
37     int img_number;
38     int is_pipe;
39     int split_planes;       /**< use independent file for each Y, U, V plane */
40     char path[1024];
41     char tmp[1024];
42     int update;
43     int use_strftime;
44     const char *muxer;
45 } VideoMuxData;
46
47 static int write_header(AVFormatContext *s)
48 {
49     VideoMuxData *img = s->priv_data;
50     AVStream *st = s->streams[0];
51     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(st->codec->pix_fmt);
52
53     av_strlcpy(img->path, s->filename, sizeof(img->path));
54     snprintf(img->tmp, sizeof(img->tmp), "%s.tmp", s->filename);
55
56     /* find format */
57     if (s->oformat->flags & AVFMT_NOFILE)
58         img->is_pipe = 0;
59     else
60         img->is_pipe = 1;
61
62     if (st->codec->codec_id == AV_CODEC_ID_GIF) {
63         img->muxer = "gif";
64     } else if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
65         const char *str = strrchr(img->path, '.');
66         img->split_planes =     str
67                              && !av_strcasecmp(str + 1, "y")
68                              && s->nb_streams == 1
69                              && desc
70                              &&(desc->flags & AV_PIX_FMT_FLAG_PLANAR)
71                              && desc->nb_components >= 3;
72     }
73     return 0;
74 }
75
76 static int write_packet(AVFormatContext *s, AVPacket *pkt)
77 {
78     VideoMuxData *img = s->priv_data;
79     AVIOContext *pb[4];
80     char filename[1024];
81     AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
82     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(codec->pix_fmt);
83     int i;
84
85     if (!img->is_pipe) {
86         if (img->update) {
87             av_strlcpy(filename, img->path, sizeof(filename));
88         } else if (img->use_strftime) {
89             time_t now0;
90             struct tm *tm, tmpbuf;
91             time(&now0);
92             tm = localtime_r(&now0, &tmpbuf);
93             if (!strftime(filename, sizeof(filename), img->path, tm)) {
94                 av_log(s, AV_LOG_ERROR, "Could not get frame filename with strftime\n");
95                 return AVERROR(EINVAL);
96             }
97         } else if (av_get_frame_filename(filename, sizeof(filename), img->path, img->img_number) < 0 &&
98                    img->img_number > 1) {
99             av_log(s, AV_LOG_ERROR,
100                    "Could not get frame filename number %d from pattern '%s' (either set updatefirst or use a pattern like %%03d within the filename pattern)\n",
101                    img->img_number, img->path);
102             return AVERROR(EINVAL);
103         }
104         for (i = 0; i < 4; i++) {
105             if (avio_open2(&pb[i], img->tmp, AVIO_FLAG_WRITE,
106                            &s->interrupt_callback, NULL) < 0) {
107                 av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", img->tmp);
108                 return AVERROR(EIO);
109             }
110
111             if (!img->split_planes || i+1 >= desc->nb_components)
112                 break;
113             filename[strlen(filename) - 1] = "UVAx"[i];
114         }
115     } else {
116         pb[0] = s->pb;
117     }
118
119     if (img->split_planes) {
120         int ysize = codec->width * codec->height;
121         int usize = FF_CEIL_RSHIFT(codec->width, desc->log2_chroma_w) * FF_CEIL_RSHIFT(codec->height, desc->log2_chroma_h);
122         if (desc->comp[0].depth >= 9) {
123             ysize *= 2;
124             usize *= 2;
125         }
126         avio_write(pb[0], pkt->data                , ysize);
127         avio_write(pb[1], pkt->data + ysize        , usize);
128         avio_write(pb[2], pkt->data + ysize + usize, usize);
129         avio_closep(&pb[1]);
130         avio_closep(&pb[2]);
131         if (desc->nb_components > 3) {
132             avio_write(pb[3], pkt->data + ysize + 2*usize, ysize);
133             avio_closep(&pb[3]);
134         }
135     } else if (img->muxer) {
136         int ret;
137         AVStream *st;
138         AVPacket pkt2 = {0};
139         AVFormatContext *fmt = NULL;
140
141         av_assert0(!img->split_planes);
142
143         ret = avformat_alloc_output_context2(&fmt, NULL, img->muxer, s->filename);
144         if (ret < 0)
145             return ret;
146         st = avformat_new_stream(fmt, NULL);
147         if (!st) {
148             avformat_free_context(fmt);
149             return AVERROR(ENOMEM);
150         }
151         st->id = pkt->stream_index;
152
153         fmt->pb = pb[0];
154         if ((ret = av_copy_packet(&pkt2, pkt))                            < 0 ||
155             (ret = av_dup_packet(&pkt2))                                  < 0 ||
156             (ret = avcodec_copy_context(st->codec, s->streams[0]->codec)) < 0 ||
157             (ret = avformat_write_header(fmt, NULL))                      < 0 ||
158             (ret = av_interleaved_write_frame(fmt, &pkt2))                < 0 ||
159             (ret = av_write_trailer(fmt))                                 < 0) {
160             av_packet_unref(&pkt2);
161             avformat_free_context(fmt);
162             return ret;
163         }
164         av_packet_unref(&pkt2);
165         avformat_free_context(fmt);
166     } else {
167         avio_write(pb[0], pkt->data, pkt->size);
168     }
169     avio_flush(pb[0]);
170     if (!img->is_pipe) {
171         avio_closep(&pb[0]);
172         ff_rename(img->tmp, filename, s);
173     }
174
175     img->img_number++;
176     return 0;
177 }
178
179 static int query_codec(enum AVCodecID id, int std_compliance)
180 {
181     int i;
182     for (i = 0; ff_img_tags[i].id != AV_CODEC_ID_NONE; i++)
183         if (ff_img_tags[i].id == id)
184             return 1;
185
186     // Anything really can be stored in img2
187     return std_compliance < FF_COMPLIANCE_NORMAL;
188 }
189
190 #define OFFSET(x) offsetof(VideoMuxData, x)
191 #define ENC AV_OPT_FLAG_ENCODING_PARAM
192 static const AVOption muxoptions[] = {
193     { "updatefirst",  "continuously overwrite one file", OFFSET(update),  AV_OPT_TYPE_INT, { .i64 = 0 }, 0,       1, ENC },
194     { "update",       "continuously overwrite one file", OFFSET(update),  AV_OPT_TYPE_INT, { .i64 = 0 }, 0,       1, ENC },
195     { "start_number", "set first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT,  { .i64 = 1 }, 0, INT_MAX, ENC },
196     { "strftime",     "use strftime for filename", OFFSET(use_strftime), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
197     { NULL },
198 };
199
200 #if CONFIG_IMAGE2_MUXER
201 static const AVClass img2mux_class = {
202     .class_name = "image2 muxer",
203     .item_name  = av_default_item_name,
204     .option     = muxoptions,
205     .version    = LIBAVUTIL_VERSION_INT,
206 };
207
208 AVOutputFormat ff_image2_muxer = {
209     .name           = "image2",
210     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
211     .extensions     = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
212                       "ppm,sgi,tga,tif,tiff,jp2,j2c,j2k,xwd,sun,ras,rs,im1,im8,im24,"
213                       "sunras,xbm,xface,pix,y",
214     .priv_data_size = sizeof(VideoMuxData),
215     .video_codec    = AV_CODEC_ID_MJPEG,
216     .write_header   = write_header,
217     .write_packet   = write_packet,
218     .query_codec    = query_codec,
219     .flags          = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
220     .priv_class     = &img2mux_class,
221 };
222 #endif
223 #if CONFIG_IMAGE2PIPE_MUXER
224 AVOutputFormat ff_image2pipe_muxer = {
225     .name           = "image2pipe",
226     .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
227     .priv_data_size = sizeof(VideoMuxData),
228     .video_codec    = AV_CODEC_ID_MJPEG,
229     .write_header   = write_header,
230     .write_packet   = write_packet,
231     .query_codec    = query_codec,
232     .flags          = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
233 };
234 #endif