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 "avformat.h"
00028 #include "avio_internal.h"
00029 #include "internal.h"
00030 #include "libavutil/opt.h"
00031
00032 typedef struct {
00033 const AVClass *class;
00034 int img_number;
00035 int is_pipe;
00036 int split_planes;
00037 char path[1024];
00038 int updatefirst;
00039 } VideoMuxData;
00040
00041 static int write_header(AVFormatContext *s)
00042 {
00043 VideoMuxData *img = s->priv_data;
00044 const char *str;
00045
00046 av_strlcpy(img->path, s->filename, sizeof(img->path));
00047
00048
00049 if (s->oformat->flags & AVFMT_NOFILE)
00050 img->is_pipe = 0;
00051 else
00052 img->is_pipe = 1;
00053
00054 str = strrchr(img->path, '.');
00055 img->split_planes = str && !av_strcasecmp(str + 1, "y");
00056 return 0;
00057 }
00058
00059 static int write_packet(AVFormatContext *s, AVPacket *pkt)
00060 {
00061 VideoMuxData *img = s->priv_data;
00062 AVIOContext *pb[3];
00063 char filename[1024];
00064 AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
00065 int i;
00066
00067 if (!img->is_pipe) {
00068 if (av_get_frame_filename(filename, sizeof(filename),
00069 img->path, img->img_number) < 0 && img->img_number>1 && !img->updatefirst) {
00070 av_log(s, AV_LOG_ERROR,
00071 "Could not get frame filename number %d from pattern '%s'\n",
00072 img->img_number, img->path);
00073 return AVERROR(EINVAL);
00074 }
00075 for(i=0; i<3; i++){
00076 if (avio_open2(&pb[i], filename, AVIO_FLAG_WRITE,
00077 &s->interrupt_callback, NULL) < 0) {
00078 av_log(s, AV_LOG_ERROR, "Could not open file : %s\n",filename);
00079 return AVERROR(EIO);
00080 }
00081
00082 if(!img->split_planes)
00083 break;
00084 filename[ strlen(filename) - 1 ]= 'U' + i;
00085 }
00086 } else {
00087 pb[0] = s->pb;
00088 }
00089
00090 if(img->split_planes){
00091 int ysize = codec->width * codec->height;
00092 avio_write(pb[0], pkt->data , ysize);
00093 avio_write(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
00094 avio_write(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
00095 avio_close(pb[1]);
00096 avio_close(pb[2]);
00097 }else{
00098 if(ff_guess_image2_codec(s->filename) == AV_CODEC_ID_JPEG2000){
00099 AVStream *st = s->streams[0];
00100 if(st->codec->extradata_size > 8 &&
00101 AV_RL32(st->codec->extradata+4) == MKTAG('j','p','2','h')){
00102 if(pkt->size < 8 || AV_RL32(pkt->data+4) != MKTAG('j','p','2','c'))
00103 goto error;
00104 avio_wb32(pb[0], 12);
00105 ffio_wfourcc(pb[0], "jP ");
00106 avio_wb32(pb[0], 0x0D0A870A);
00107 avio_wb32(pb[0], 20);
00108 ffio_wfourcc(pb[0], "ftyp");
00109 ffio_wfourcc(pb[0], "jp2 ");
00110 avio_wb32(pb[0], 0);
00111 ffio_wfourcc(pb[0], "jp2 ");
00112 avio_write(pb[0], st->codec->extradata, st->codec->extradata_size);
00113 }else if(pkt->size >= 8 && AV_RB32(pkt->data) == 0xFF4FFF51){
00114
00115 }else if(pkt->size < 8 ||
00116 (!st->codec->extradata_size &&
00117 AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){
00118 error:
00119 av_log(s, AV_LOG_ERROR, "malformed JPEG 2000 codestream %X\n", AV_RB32(pkt->data));
00120 return -1;
00121 }
00122 }
00123 avio_write(pb[0], pkt->data, pkt->size);
00124 }
00125 avio_flush(pb[0]);
00126 if (!img->is_pipe) {
00127 avio_close(pb[0]);
00128 }
00129
00130 img->img_number++;
00131 return 0;
00132 }
00133
00134 #define OFFSET(x) offsetof(VideoMuxData, x)
00135 #define ENC AV_OPT_FLAG_ENCODING_PARAM
00136 static const AVOption muxoptions[] = {
00137 { "updatefirst", "", OFFSET(updatefirst), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, ENC },
00138 { "start_number", "first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, ENC },
00139 { NULL },
00140 };
00141
00142 #if CONFIG_IMAGE2_MUXER
00143 static const AVClass img2mux_class = {
00144 .class_name = "image2 muxer",
00145 .item_name = av_default_item_name,
00146 .option = muxoptions,
00147 .version = LIBAVUTIL_VERSION_INT,
00148 };
00149
00150 AVOutputFormat ff_image2_muxer = {
00151 .name = "image2",
00152 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
00153 .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
00154 "ppm,sgi,tga,tif,tiff,jp2,j2c,xwd,sun,ras,rs,im1,im8,im24,"
00155 "sunras,xbm",
00156 .priv_data_size = sizeof(VideoMuxData),
00157 .video_codec = AV_CODEC_ID_MJPEG,
00158 .write_header = write_header,
00159 .write_packet = write_packet,
00160 .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
00161 .priv_class = &img2mux_class,
00162 };
00163 #endif
00164 #if CONFIG_IMAGE2PIPE_MUXER
00165 AVOutputFormat ff_image2pipe_muxer = {
00166 .name = "image2pipe",
00167 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
00168 .priv_data_size = sizeof(VideoMuxData),
00169 .video_codec = AV_CODEC_ID_MJPEG,
00170 .write_header = write_header,
00171 .write_packet = write_packet,
00172 .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
00173 };
00174 #endif