00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022
00023 #define Y4M_MAGIC "YUV4MPEG2"
00024 #define Y4M_FRAME_MAGIC "FRAME"
00025 #define Y4M_LINE_MAX 256
00026
00027 struct frame_attributes {
00028 int interlaced_frame;
00029 int top_field_first;
00030 };
00031
00032 #if CONFIG_YUV4MPEGPIPE_MUXER
00033 static int yuv4_generate_header(AVFormatContext *s, char* buf)
00034 {
00035 AVStream *st;
00036 int width, height;
00037 int raten, rated, aspectn, aspectd, n;
00038 char inter;
00039 const char *colorspace = "";
00040
00041 st = s->streams[0];
00042 width = st->codec->width;
00043 height = st->codec->height;
00044
00045 av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
00046
00047 aspectn = st->sample_aspect_ratio.num;
00048 aspectd = st->sample_aspect_ratio.den;
00049
00050 if ( aspectn == 0 && aspectd == 1 ) aspectd = 0;
00051
00052 inter = 'p';
00053 if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
00054 inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
00055 }
00056
00057 switch(st->codec->pix_fmt) {
00058 case PIX_FMT_GRAY8:
00059 colorspace = " Cmono";
00060 break;
00061 case PIX_FMT_YUV411P:
00062 colorspace = " C411 XYSCSS=411";
00063 break;
00064 case PIX_FMT_YUV420P:
00065 colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
00066 break;
00067 case PIX_FMT_YUV422P:
00068 colorspace = " C422 XYSCSS=422";
00069 break;
00070 case PIX_FMT_YUV444P:
00071 colorspace = " C444 XYSCSS=444";
00072 break;
00073 }
00074
00075
00076 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
00077 Y4M_MAGIC,
00078 width,
00079 height,
00080 raten, rated,
00081 inter,
00082 aspectn, aspectd,
00083 colorspace);
00084
00085 return n;
00086 }
00087
00088 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
00089 {
00090 AVStream *st = s->streams[pkt->stream_index];
00091 ByteIOContext *pb = s->pb;
00092 AVPicture *picture;
00093 int* first_pkt = s->priv_data;
00094 int width, height, h_chroma_shift, v_chroma_shift;
00095 int i, m;
00096 char buf2[Y4M_LINE_MAX+1];
00097 char buf1[20];
00098 uint8_t *ptr, *ptr1, *ptr2;
00099
00100 picture = (AVPicture *)pkt->data;
00101
00102
00103 if (*first_pkt) {
00104 *first_pkt = 0;
00105 if (yuv4_generate_header(s, buf2) < 0) {
00106 av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
00107 return AVERROR(EIO);
00108 } else {
00109 put_buffer(pb, buf2, strlen(buf2));
00110 }
00111 }
00112
00113
00114
00115 m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
00116 put_buffer(pb, buf1, strlen(buf1));
00117
00118 width = st->codec->width;
00119 height = st->codec->height;
00120
00121 ptr = picture->data[0];
00122 for(i=0;i<height;i++) {
00123 put_buffer(pb, ptr, width);
00124 ptr += picture->linesize[0];
00125 }
00126
00127 if (st->codec->pix_fmt != PIX_FMT_GRAY8){
00128
00129 avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
00130 width >>= h_chroma_shift;
00131 height >>= v_chroma_shift;
00132
00133 ptr1 = picture->data[1];
00134 ptr2 = picture->data[2];
00135 for(i=0;i<height;i++) {
00136 put_buffer(pb, ptr1, width);
00137 ptr1 += picture->linesize[1];
00138 }
00139 for(i=0;i<height;i++) {
00140 put_buffer(pb, ptr2, width);
00141 ptr2 += picture->linesize[2];
00142 }
00143 }
00144 put_flush_packet(pb);
00145 return 0;
00146 }
00147
00148 static int yuv4_write_header(AVFormatContext *s)
00149 {
00150 int* first_pkt = s->priv_data;
00151
00152 if (s->nb_streams != 1)
00153 return AVERROR(EIO);
00154
00155 if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
00156 av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
00157 }
00158 else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
00159 (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
00160 (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
00161 (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
00162 av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
00163 return AVERROR(EIO);
00164 }
00165
00166 *first_pkt = 1;
00167 return 0;
00168 }
00169
00170 AVOutputFormat yuv4mpegpipe_muxer = {
00171 "yuv4mpegpipe",
00172 NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
00173 "",
00174 "y4m",
00175 sizeof(int),
00176 CODEC_ID_NONE,
00177 CODEC_ID_RAWVIDEO,
00178 yuv4_write_header,
00179 yuv4_write_packet,
00180 .flags = AVFMT_RAWPICTURE,
00181 };
00182 #endif
00183
00184
00185 #define MAX_YUV4_HEADER 80
00186 #define MAX_FRAME_HEADER 80
00187
00188 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
00189 {
00190 char header[MAX_YUV4_HEADER+10];
00191 char *tokstart,*tokend,*header_end;
00192 int i;
00193 ByteIOContext *pb = s->pb;
00194 int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
00195 enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
00196 AVStream *st;
00197 struct frame_attributes *s1 = s->priv_data;
00198
00199 for (i=0; i<MAX_YUV4_HEADER; i++) {
00200 header[i] = get_byte(pb);
00201 if (header[i] == '\n') {
00202 header[i+1] = 0x20;
00203 header[i+2] = 0;
00204 break;
00205 }
00206 }
00207 if (i == MAX_YUV4_HEADER) return -1;
00208 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
00209
00210 s1->interlaced_frame = 0;
00211 s1->top_field_first = 0;
00212 header_end = &header[i+1];
00213 for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
00214 if (*tokstart==0x20) continue;
00215 switch (*tokstart++) {
00216 case 'W':
00217 width = strtol(tokstart, &tokend, 10);
00218 tokstart=tokend;
00219 break;
00220 case 'H':
00221 height = strtol(tokstart, &tokend, 10);
00222 tokstart=tokend;
00223 break;
00224 case 'C':
00225 if (strncmp("420jpeg",tokstart,7)==0)
00226 pix_fmt = PIX_FMT_YUV420P;
00227 else if (strncmp("420mpeg2",tokstart,8)==0)
00228 pix_fmt = PIX_FMT_YUV420P;
00229 else if (strncmp("420paldv", tokstart, 8)==0)
00230 pix_fmt = PIX_FMT_YUV420P;
00231 else if (strncmp("411", tokstart, 3)==0)
00232 pix_fmt = PIX_FMT_YUV411P;
00233 else if (strncmp("422", tokstart, 3)==0)
00234 pix_fmt = PIX_FMT_YUV422P;
00235 else if (strncmp("444alpha", tokstart, 8)==0) {
00236 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
00237 return -1;
00238 } else if (strncmp("444", tokstart, 3)==0)
00239 pix_fmt = PIX_FMT_YUV444P;
00240 else if (strncmp("mono",tokstart, 4)==0) {
00241 pix_fmt = PIX_FMT_GRAY8;
00242 } else {
00243 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
00244 return -1;
00245 }
00246 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
00247 break;
00248 case 'I':
00249 switch (*tokstart++){
00250 case '?':
00251 break;
00252 case 'p':
00253 s1->interlaced_frame=0;
00254 break;
00255 case 't':
00256 s1->interlaced_frame=1;
00257 s1->top_field_first=1;
00258 break;
00259 case 'b':
00260 s1->interlaced_frame=1;
00261 s1->top_field_first=0;
00262 break;
00263 case 'm':
00264 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
00265 return -1;
00266 default:
00267 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
00268 return -1;
00269 }
00270 break;
00271 case 'F':
00272 sscanf(tokstart,"%d:%d",&raten,&rated);
00273 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
00274 break;
00275 case 'A':
00276 sscanf(tokstart,"%d:%d",&aspectn,&aspectd);
00277 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
00278 break;
00279 case 'X':
00280 if (strncmp("YSCSS=",tokstart,6)==0) {
00281
00282 tokstart+=6;
00283 if (strncmp("420JPEG",tokstart,7)==0)
00284 alt_pix_fmt=PIX_FMT_YUV420P;
00285 else if (strncmp("420MPEG2",tokstart,8)==0)
00286 alt_pix_fmt=PIX_FMT_YUV420P;
00287 else if (strncmp("420PALDV",tokstart,8)==0)
00288 alt_pix_fmt=PIX_FMT_YUV420P;
00289 else if (strncmp("411",tokstart,3)==0)
00290 alt_pix_fmt=PIX_FMT_YUV411P;
00291 else if (strncmp("422",tokstart,3)==0)
00292 alt_pix_fmt=PIX_FMT_YUV422P;
00293 else if (strncmp("444",tokstart,3)==0)
00294 alt_pix_fmt=PIX_FMT_YUV444P;
00295 }
00296 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
00297 break;
00298 }
00299 }
00300
00301 if ((width == -1) || (height == -1)) {
00302 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
00303 return -1;
00304 }
00305
00306 if (pix_fmt == PIX_FMT_NONE) {
00307 if (alt_pix_fmt == PIX_FMT_NONE)
00308 pix_fmt = PIX_FMT_YUV420P;
00309 else
00310 pix_fmt = alt_pix_fmt;
00311 }
00312
00313 if (raten == 0 && rated == 0) {
00314
00315 raten = 25;
00316 rated = 1;
00317 }
00318
00319 if (aspectn == 0 && aspectd == 0) {
00320
00321 aspectd = 1;
00322 }
00323
00324 st = av_new_stream(s, 0);
00325 if(!st)
00326 return AVERROR(ENOMEM);
00327 st->codec->width = width;
00328 st->codec->height = height;
00329 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
00330 av_set_pts_info(st, 64, rated, raten);
00331 st->codec->pix_fmt = pix_fmt;
00332 st->codec->codec_type = CODEC_TYPE_VIDEO;
00333 st->codec->codec_id = CODEC_ID_RAWVIDEO;
00334 st->sample_aspect_ratio= (AVRational){aspectn, aspectd};
00335
00336 return 0;
00337 }
00338
00339 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
00340 {
00341 int i;
00342 char header[MAX_FRAME_HEADER+1];
00343 int packet_size, width, height;
00344 AVStream *st = s->streams[0];
00345 struct frame_attributes *s1 = s->priv_data;
00346
00347 for (i=0; i<MAX_FRAME_HEADER; i++) {
00348 header[i] = get_byte(s->pb);
00349 if (header[i] == '\n') {
00350 header[i+1] = 0;
00351 break;
00352 }
00353 }
00354 if (i == MAX_FRAME_HEADER) return -1;
00355 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
00356
00357 width = st->codec->width;
00358 height = st->codec->height;
00359
00360 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
00361 if (packet_size < 0)
00362 return -1;
00363
00364 if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
00365 return AVERROR(EIO);
00366
00367 if (s->streams[0]->codec->coded_frame) {
00368 s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
00369 s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
00370 }
00371
00372 pkt->stream_index = 0;
00373 return 0;
00374 }
00375
00376 static int yuv4_probe(AVProbeData *pd)
00377 {
00378
00379 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
00380 return AVPROBE_SCORE_MAX;
00381 else
00382 return 0;
00383 }
00384
00385 #if CONFIG_YUV4MPEGPIPE_DEMUXER
00386 AVInputFormat yuv4mpegpipe_demuxer = {
00387 "yuv4mpegpipe",
00388 NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
00389 sizeof(struct frame_attributes),
00390 yuv4_probe,
00391 yuv4_read_header,
00392 yuv4_read_packet,
00393 .extensions = "y4m"
00394 };
00395 #endif