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
00031 typedef struct {
00032 const AVClass *class;
00033 int img_number;
00034 int is_pipe;
00035 int split_planes;
00036 char path[1024];
00037 int updatefirst;
00038 } VideoMuxData;
00039
00040 static int write_header(AVFormatContext *s)
00041 {
00042 VideoMuxData *img = s->priv_data;
00043 const char *str;
00044
00045 img->img_number = 1;
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_flush(pb[1]);
00096 avio_flush(pb[2]);
00097 avio_close(pb[1]);
00098 avio_close(pb[2]);
00099 }else{
00100 if(ff_guess_image2_codec(s->filename) == CODEC_ID_JPEG2000){
00101 AVStream *st = s->streams[0];
00102 if(st->codec->extradata_size > 8 &&
00103 AV_RL32(st->codec->extradata+4) == MKTAG('j','p','2','h')){
00104 if(pkt->size < 8 || AV_RL32(pkt->data+4) != MKTAG('j','p','2','c'))
00105 goto error;
00106 avio_wb32(pb[0], 12);
00107 ffio_wfourcc(pb[0], "jP ");
00108 avio_wb32(pb[0], 0x0D0A870A);
00109 avio_wb32(pb[0], 20);
00110 ffio_wfourcc(pb[0], "ftyp");
00111 ffio_wfourcc(pb[0], "jp2 ");
00112 avio_wb32(pb[0], 0);
00113 ffio_wfourcc(pb[0], "jp2 ");
00114 avio_write(pb[0], st->codec->extradata, st->codec->extradata_size);
00115 }else if(pkt->size >= 8 && AV_RB32(pkt->data) == 0xFF4FFF51){
00116
00117 }else if(pkt->size < 8 ||
00118 (!st->codec->extradata_size &&
00119 AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){
00120 error:
00121 av_log(s, AV_LOG_ERROR, "malformed JPEG 2000 codestream %X\n", AV_RB32(pkt->data));
00122 return -1;
00123 }
00124 }
00125 avio_write(pb[0], pkt->data, pkt->size);
00126 }
00127 avio_flush(pb[0]);
00128 if (!img->is_pipe) {
00129 avio_close(pb[0]);
00130 }
00131
00132 img->img_number++;
00133 return 0;
00134 }
00135
00136 #define OFFSET(x) offsetof(VideoMuxData, x)
00137 #define ENC AV_OPT_FLAG_ENCODING_PARAM
00138 static const AVOption muxoptions[] = {
00139 { "updatefirst", "", OFFSET(updatefirst), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, ENC },
00140 { NULL },
00141 };
00142
00143
00144 #if CONFIG_IMAGE2_MUXER
00145 static const AVClass img2mux_class = {
00146 .class_name = "image2 muxer",
00147 .item_name = av_default_item_name,
00148 .option = muxoptions,
00149 .version = LIBAVUTIL_VERSION_INT,
00150 };
00151 AVOutputFormat ff_image2_muxer = {
00152 .name = "image2",
00153 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
00154 .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
00155 "ppm,sgi,tga,tif,tiff,jp2,j2c,xwd,sun,ras,rs,im1,im8,im24,"
00156 "sunras,xbm",
00157 .priv_data_size = sizeof(VideoMuxData),
00158 .video_codec = CODEC_ID_MJPEG,
00159 .write_header = write_header,
00160 .write_packet = write_packet,
00161 .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
00162 .priv_class = &img2mux_class,
00163 };
00164 #endif
00165 #if CONFIG_IMAGE2PIPE_MUXER
00166 AVOutputFormat ff_image2pipe_muxer = {
00167 .name = "image2pipe",
00168 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
00169 .priv_data_size = sizeof(VideoMuxData),
00170 .video_codec = CODEC_ID_MJPEG,
00171 .write_header = write_header,
00172 .write_packet = write_packet,
00173 .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
00174 };
00175 #endif