00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/avstring.h"
00025 #include "libavutil/log.h"
00026 #include "libavutil/opt.h"
00027 #include "libavutil/pixdesc.h"
00028 #include "avformat.h"
00029 #include "avio_internal.h"
00030 #include "internal.h"
00031 #include "libavutil/opt.h"
00032
00033 typedef struct {
00034 const AVClass *class;
00035 int img_number;
00036 int is_pipe;
00037 int split_planes;
00038 char path[1024];
00039 int updatefirst;
00040 } VideoMuxData;
00041
00042 static int write_header(AVFormatContext *s)
00043 {
00044 VideoMuxData *img = s->priv_data;
00045 AVStream *st = s->streams[0];
00046 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(st->codec->pix_fmt);
00047 const char *str;
00048
00049 av_strlcpy(img->path, s->filename, sizeof(img->path));
00050
00051
00052 if (s->oformat->flags & AVFMT_NOFILE)
00053 img->is_pipe = 0;
00054 else
00055 img->is_pipe = 1;
00056
00057 str = strrchr(img->path, '.');
00058 img->split_planes = str
00059 && !av_strcasecmp(str + 1, "y")
00060 && s->nb_streams == 1
00061 && st->codec->codec_id == AV_CODEC_ID_RAWVIDEO
00062 && desc
00063 &&(desc->flags & PIX_FMT_PLANAR)
00064 && desc->nb_components >= 3;
00065 return 0;
00066 }
00067
00068 static int write_packet(AVFormatContext *s, AVPacket *pkt)
00069 {
00070 VideoMuxData *img = s->priv_data;
00071 AVIOContext *pb[4];
00072 char filename[1024];
00073 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
00074 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(codec->pix_fmt);
00075 int i;
00076
00077 if (!img->is_pipe) {
00078 if (av_get_frame_filename(filename, sizeof(filename),
00079 img->path, img->img_number) < 0 && img->img_number > 1 && !img->updatefirst) {
00080 av_log(s, AV_LOG_ERROR,
00081 "Could not get frame filename number %d from pattern '%s'\n",
00082 img->img_number, img->path);
00083 return AVERROR(EINVAL);
00084 }
00085 for (i = 0; i < 4; i++) {
00086 if (avio_open2(&pb[i], filename, AVIO_FLAG_WRITE,
00087 &s->interrupt_callback, NULL) < 0) {
00088 av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", filename);
00089 return AVERROR(EIO);
00090 }
00091
00092 if (!img->split_planes || i+1 >= desc->nb_components)
00093 break;
00094 filename[strlen(filename) - 1] = ((int[]){'U','V','A','x'})[i];
00095 }
00096 } else {
00097 pb[0] = s->pb;
00098 }
00099
00100 if (img->split_planes) {
00101 int ysize = codec->width * codec->height;
00102 int usize = ((-codec->width)>>desc->log2_chroma_w) * ((-codec->height)>>desc->log2_chroma_h);
00103 if (desc->comp[0].depth_minus1 >= 8) {
00104 ysize *= 2;
00105 usize *= 2;
00106 }
00107 avio_write(pb[0], pkt->data , ysize);
00108 avio_write(pb[1], pkt->data + ysize , usize);
00109 avio_write(pb[2], pkt->data + ysize + usize, usize);
00110 avio_close(pb[1]);
00111 avio_close(pb[2]);
00112 if (desc->nb_components > 3) {
00113 avio_write(pb[3], pkt->data + ysize + 2*usize, ysize);
00114 avio_close(pb[3]);
00115 }
00116 } else {
00117 avio_write(pb[0], pkt->data, pkt->size);
00118 }
00119 avio_flush(pb[0]);
00120 if (!img->is_pipe) {
00121 avio_close(pb[0]);
00122 }
00123
00124 img->img_number++;
00125 return 0;
00126 }
00127
00128 #define OFFSET(x) offsetof(VideoMuxData, x)
00129 #define ENC AV_OPT_FLAG_ENCODING_PARAM
00130 static const AVOption muxoptions[] = {
00131 { "updatefirst", "", OFFSET(updatefirst), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
00132 { "start_number", "first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, ENC },
00133 { NULL },
00134 };
00135
00136 #if CONFIG_IMAGE2_MUXER
00137 static const AVClass img2mux_class = {
00138 .class_name = "image2 muxer",
00139 .item_name = av_default_item_name,
00140 .option = muxoptions,
00141 .version = LIBAVUTIL_VERSION_INT,
00142 };
00143
00144 AVOutputFormat ff_image2_muxer = {
00145 .name = "image2",
00146 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
00147 .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
00148 "ppm,sgi,tga,tif,tiff,jp2,j2c,xwd,sun,ras,rs,im1,im8,im24,"
00149 "sunras,xbm,xface",
00150 .priv_data_size = sizeof(VideoMuxData),
00151 .video_codec = AV_CODEC_ID_MJPEG,
00152 .write_header = write_header,
00153 .write_packet = write_packet,
00154 .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
00155 .priv_class = &img2mux_class,
00156 };
00157 #endif
00158 #if CONFIG_IMAGE2PIPE_MUXER
00159 AVOutputFormat ff_image2pipe_muxer = {
00160 .name = "image2pipe",
00161 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
00162 .priv_data_size = sizeof(VideoMuxData),
00163 .video_codec = AV_CODEC_ID_MJPEG,
00164 .write_header = write_header,
00165 .write_packet = write_packet,
00166 .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
00167 };
00168 #endif