3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * Copyright (c) 2004 Michael Niedermayer
6 * This file is part of Libav.
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.
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.
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
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/log.h"
27 #include "avio_internal.h"
29 #include "libavutil/opt.h"
31 typedef struct VideoMuxData {
32 const AVClass *class; /**< Class for private options. */
40 static int write_header(AVFormatContext *s)
42 VideoMuxData *img = s->priv_data;
44 av_strlcpy(img->path, s->filename, sizeof(img->path));
45 snprintf(img->tmp, sizeof(img->tmp), "%s.tmp", s->filename);
48 if (s->oformat->flags & AVFMT_NOFILE)
56 static int write_packet(AVFormatContext *s, AVPacket *pkt)
58 VideoMuxData *img = s->priv_data;
61 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
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);
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);
81 if (codec->codec_id != AV_CODEC_ID_RAWVIDEO)
83 filename[strlen(filename) - 1] = 'U' + i;
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);
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')) {
102 AV_RL32(pkt->data + 4) != MKTAG('j', 'p', '2', 'c'))
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 ");
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
117 av_log(s, AV_LOG_ERROR, "malformed JPEG 2000 codestream\n");
121 avio_write(pb[0], pkt->data, pkt->size);
126 ff_rename(img->tmp, filename);
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 },
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,
149 AVOutputFormat ff_image2_muxer = {
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,
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