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 static int yuv4_generate_header(AVFormatContext *s, char* buf)
00033 {
00034 AVStream *st;
00035 int width, height;
00036 int raten, rated, aspectn, aspectd, n;
00037 char inter;
00038 const char *colorspace = "";
00039
00040 st = s->streams[0];
00041 width = st->codec->width;
00042 height = st->codec->height;
00043
00044 av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
00045
00046 aspectn = st->codec->sample_aspect_ratio.num;
00047 aspectd = st->codec->sample_aspect_ratio.den;
00048
00049 if ( aspectn == 0 && aspectd == 1 ) aspectd = 0;
00050
00051 inter = 'p';
00052 if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
00053 inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
00054 }
00055
00056 switch(st->codec->pix_fmt) {
00057 case PIX_FMT_GRAY8:
00058 colorspace = " Cmono";
00059 break;
00060 case PIX_FMT_YUV411P:
00061 colorspace = " C411 XYSCSS=411";
00062 break;
00063 case PIX_FMT_YUV420P:
00064 colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
00065 break;
00066 case PIX_FMT_YUV422P:
00067 colorspace = " C422 XYSCSS=422";
00068 break;
00069 case PIX_FMT_YUV444P:
00070 colorspace = " C444 XYSCSS=444";
00071 break;
00072 }
00073
00074
00075 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
00076 Y4M_MAGIC,
00077 width,
00078 height,
00079 raten, rated,
00080 inter,
00081 aspectn, aspectd,
00082 colorspace);
00083
00084 return n;
00085 }
00086
00087 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
00088 {
00089 AVStream *st = s->streams[pkt->stream_index];
00090 ByteIOContext *pb = s->pb;
00091 AVPicture *picture;
00092 int* first_pkt = s->priv_data;
00093 int width, height, h_chroma_shift, v_chroma_shift;
00094 int i, m;
00095 char buf2[Y4M_LINE_MAX+1];
00096 char buf1[20];
00097 uint8_t *ptr, *ptr1, *ptr2;
00098
00099 picture = (AVPicture *)pkt->data;
00100
00101
00102 if (*first_pkt) {
00103 *first_pkt = 0;
00104 if (yuv4_generate_header(s, buf2) < 0) {
00105 av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
00106 return AVERROR(EIO);
00107 } else {
00108 put_buffer(pb, buf2, strlen(buf2));
00109 }
00110 }
00111
00112
00113
00114 m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
00115 put_buffer(pb, buf1, strlen(buf1));
00116
00117 width = st->codec->width;
00118 height = st->codec->height;
00119
00120 ptr = picture->data[0];
00121 for(i=0;i<height;i++) {
00122 put_buffer(pb, ptr, width);
00123 ptr += picture->linesize[0];
00124 }
00125
00126 if (st->codec->pix_fmt != PIX_FMT_GRAY8){
00127
00128 avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
00129 width >>= h_chroma_shift;
00130 height >>= v_chroma_shift;
00131
00132 ptr1 = picture->data[1];
00133 ptr2 = picture->data[2];
00134 for(i=0;i<height;i++) {
00135 put_buffer(pb, ptr1, width);
00136 ptr1 += picture->linesize[1];
00137 }
00138 for(i=0;i<height;i++) {
00139 put_buffer(pb, ptr2, width);
00140 ptr2 += picture->linesize[2];
00141 }
00142 }
00143 put_flush_packet(pb);
00144 return 0;
00145 }
00146
00147 static int yuv4_write_header(AVFormatContext *s)
00148 {
00149 int* first_pkt = s->priv_data;
00150
00151 if (s->nb_streams != 1)
00152 return AVERROR(EIO);
00153
00154 if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
00155 av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
00156 }
00157 else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
00158 (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
00159 (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
00160 (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
00161 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");
00162 return AVERROR(EIO);
00163 }
00164
00165 *first_pkt = 1;
00166 return 0;
00167 }
00168
00169 #ifdef CONFIG_YUV4MPEGPIPE_MUXER
00170 AVOutputFormat yuv4mpegpipe_muxer = {
00171 "yuv4mpegpipe",
00172 "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 st = s->streams[0];
00326 st->codec->width = width;
00327 st->codec->height = height;
00328 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
00329 av_set_pts_info(st, 64, rated, raten);
00330 st->codec->pix_fmt = pix_fmt;
00331 st->codec->codec_type = CODEC_TYPE_VIDEO;
00332 st->codec->codec_id = CODEC_ID_RAWVIDEO;
00333 st->codec->sample_aspect_ratio= (AVRational){aspectn, aspectd};
00334
00335 return 0;
00336 }
00337
00338 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
00339 {
00340 int i;
00341 char header[MAX_FRAME_HEADER+1];
00342 int packet_size, width, height;
00343 AVStream *st = s->streams[0];
00344 struct frame_attributes *s1 = s->priv_data;
00345
00346 for (i=0; i<MAX_FRAME_HEADER; i++) {
00347 header[i] = get_byte(s->pb);
00348 if (header[i] == '\n') {
00349 header[i+1] = 0;
00350 break;
00351 }
00352 }
00353 if (i == MAX_FRAME_HEADER) return -1;
00354 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
00355
00356 width = st->codec->width;
00357 height = st->codec->height;
00358
00359 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
00360 if (packet_size < 0)
00361 return -1;
00362
00363 if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
00364 return AVERROR(EIO);
00365
00366 if (s->streams[0]->codec->coded_frame) {
00367 s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
00368 s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
00369 }
00370
00371 pkt->stream_index = 0;
00372 return 0;
00373 }
00374
00375 static int yuv4_read_close(AVFormatContext *s)
00376 {
00377 return 0;
00378 }
00379
00380 static int yuv4_probe(AVProbeData *pd)
00381 {
00382
00383 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
00384 return AVPROBE_SCORE_MAX;
00385 else
00386 return 0;
00387 }
00388
00389 #ifdef CONFIG_YUV4MPEGPIPE_DEMUXER
00390 AVInputFormat yuv4mpegpipe_demuxer = {
00391 "yuv4mpegpipe",
00392 "YUV4MPEG pipe format",
00393 sizeof(struct frame_attributes),
00394 yuv4_probe,
00395 yuv4_read_header,
00396 yuv4_read_packet,
00397 yuv4_read_close,
00398 .extensions = "y4m"
00399 };
00400 #endif