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 #include "internal.h"
00023 #include "libavutil/pixdesc.h"
00024
00025 #define Y4M_MAGIC "YUV4MPEG2"
00026 #define Y4M_FRAME_MAGIC "FRAME"
00027 #define Y4M_LINE_MAX 256
00028
00029 struct frame_attributes {
00030 int interlaced_frame;
00031 int top_field_first;
00032 };
00033
00034 #if CONFIG_YUV4MPEGPIPE_MUXER
00035 static int yuv4_generate_header(AVFormatContext *s, char* buf)
00036 {
00037 AVStream *st;
00038 int width, height;
00039 int raten, rated, aspectn, aspectd, n;
00040 char inter;
00041 const char *colorspace = "";
00042
00043 st = s->streams[0];
00044 width = st->codec->width;
00045 height = st->codec->height;
00046
00047 av_reduce(&raten, &rated, st->codec->time_base.den,
00048 st->codec->time_base.num, (1UL << 31) - 1);
00049
00050 aspectn = st->sample_aspect_ratio.num;
00051 aspectd = st->sample_aspect_ratio.den;
00052
00053 if (aspectn == 0 && aspectd == 1)
00054 aspectd = 0;
00055
00056 inter = 'p';
00057 if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame)
00058 inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
00059
00060 switch (st->codec->pix_fmt) {
00061 case PIX_FMT_GRAY8:
00062 colorspace = " Cmono";
00063 break;
00064 case PIX_FMT_GRAY16:
00065 colorspace = " Cmono16";
00066 break;
00067 case PIX_FMT_YUV411P:
00068 colorspace = " C411 XYSCSS=411";
00069 break;
00070 case PIX_FMT_YUV420P:
00071 switch (st->codec->chroma_sample_location) {
00072 case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
00073 case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
00074 default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
00075 }
00076 break;
00077 case PIX_FMT_YUV422P:
00078 colorspace = " C422 XYSCSS=422";
00079 break;
00080 case PIX_FMT_YUV444P:
00081 colorspace = " C444 XYSCSS=444";
00082 break;
00083 case PIX_FMT_YUV420P9:
00084 colorspace = " C420p9 XYSCSS=420P9";
00085 break;
00086 case PIX_FMT_YUV422P9:
00087 colorspace = " C422p9 XYSCSS=422P9";
00088 break;
00089 case PIX_FMT_YUV444P9:
00090 colorspace = " C444p9 XYSCSS=444P9";
00091 break;
00092 case PIX_FMT_YUV420P10:
00093 colorspace = " C420p10 XYSCSS=420P10";
00094 break;
00095 case PIX_FMT_YUV422P10:
00096 colorspace = " C422p10 XYSCSS=422P10";
00097 break;
00098 case PIX_FMT_YUV444P10:
00099 colorspace = " C444p10 XYSCSS=444P10";
00100 break;
00101 case PIX_FMT_YUV420P16:
00102 colorspace = " C420p16 XYSCSS=420P16";
00103 break;
00104 case PIX_FMT_YUV422P16:
00105 colorspace = " C422p16 XYSCSS=422P16";
00106 break;
00107 case PIX_FMT_YUV444P16:
00108 colorspace = " C444p16 XYSCSS=444P16";
00109 break;
00110 }
00111
00112
00113 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
00114 Y4M_MAGIC, width, height, raten, rated, inter,
00115 aspectn, aspectd, colorspace);
00116
00117 return n;
00118 }
00119
00120 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
00121 {
00122 AVStream *st = s->streams[pkt->stream_index];
00123 AVIOContext *pb = s->pb;
00124 AVPicture *picture;
00125 int* first_pkt = s->priv_data;
00126 int width, height, h_chroma_shift, v_chroma_shift;
00127 int i;
00128 char buf2[Y4M_LINE_MAX + 1];
00129 char buf1[20];
00130 uint8_t *ptr, *ptr1, *ptr2;
00131
00132 picture = (AVPicture *)pkt->data;
00133
00134
00135 if (*first_pkt) {
00136 *first_pkt = 0;
00137 if (yuv4_generate_header(s, buf2) < 0) {
00138 av_log(s, AV_LOG_ERROR,
00139 "Error. YUV4MPEG stream header write failed.\n");
00140 return AVERROR(EIO);
00141 } else {
00142 avio_write(pb, buf2, strlen(buf2));
00143 }
00144 }
00145
00146
00147
00148 snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
00149 avio_write(pb, buf1, strlen(buf1));
00150
00151 width = st->codec->width;
00152 height = st->codec->height;
00153
00154 ptr = picture->data[0];
00155
00156 switch (st->codec->pix_fmt) {
00157 case PIX_FMT_GRAY8:
00158 case PIX_FMT_YUV411P:
00159 case PIX_FMT_YUV420P:
00160 case PIX_FMT_YUV422P:
00161 case PIX_FMT_YUV444P:
00162 break;
00163 case PIX_FMT_GRAY16:
00164 case PIX_FMT_YUV420P9:
00165 case PIX_FMT_YUV422P9:
00166 case PIX_FMT_YUV444P9:
00167 case PIX_FMT_YUV420P10:
00168 case PIX_FMT_YUV422P10:
00169 case PIX_FMT_YUV444P10:
00170 case PIX_FMT_YUV420P16:
00171 case PIX_FMT_YUV422P16:
00172 case PIX_FMT_YUV444P16:
00173 width *= 2;
00174 break;
00175 default:
00176 av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
00177 av_get_pix_fmt_name(st->codec->pix_fmt));
00178 return AVERROR(EINVAL);
00179 }
00180
00181 for (i = 0; i < height; i++) {
00182 avio_write(pb, ptr, width);
00183 ptr += picture->linesize[0];
00184 }
00185
00186 if (st->codec->pix_fmt != PIX_FMT_GRAY8 &&
00187 st->codec->pix_fmt != PIX_FMT_GRAY16) {
00188
00189 avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
00190 &v_chroma_shift);
00191 width >>= h_chroma_shift;
00192 height >>= v_chroma_shift;
00193
00194 ptr1 = picture->data[1];
00195 ptr2 = picture->data[2];
00196 for (i = 0; i < height; i++) {
00197 avio_write(pb, ptr1, width);
00198 ptr1 += picture->linesize[1];
00199 }
00200 for (i = 0; i < height; i++) {
00201 avio_write(pb, ptr2, width);
00202 ptr2 += picture->linesize[2];
00203 }
00204 }
00205
00206 avio_flush(pb);
00207 return 0;
00208 }
00209
00210 static int yuv4_write_header(AVFormatContext *s)
00211 {
00212 int *first_pkt = s->priv_data;
00213
00214 if (s->nb_streams != 1)
00215 return AVERROR(EIO);
00216
00217 if (s->streams[0]->codec->codec_id != CODEC_ID_RAWVIDEO) {
00218 av_log(s, AV_LOG_ERROR,
00219 "A non-rawvideo stream was selected, but yuv4mpeg only handles rawvideo streams\n");
00220 return AVERROR(EINVAL);
00221 }
00222
00223 switch (s->streams[0]->codec->pix_fmt) {
00224 case PIX_FMT_YUV411P:
00225 av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
00226 "stream, some mjpegtools might not work.\n");
00227 break;
00228 case PIX_FMT_GRAY8:
00229 case PIX_FMT_GRAY16:
00230 case PIX_FMT_YUV420P:
00231 case PIX_FMT_YUV422P:
00232 case PIX_FMT_YUV444P:
00233 break;
00234 case PIX_FMT_YUV420P9:
00235 case PIX_FMT_YUV422P9:
00236 case PIX_FMT_YUV444P9:
00237 case PIX_FMT_YUV420P10:
00238 case PIX_FMT_YUV422P10:
00239 case PIX_FMT_YUV444P10:
00240 case PIX_FMT_YUV420P16:
00241 case PIX_FMT_YUV422P16:
00242 case PIX_FMT_YUV444P16:
00243 if (s->streams[0]->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
00244 av_log(s, AV_LOG_ERROR, "'%s' is not a official yuv4mpegpipe pixel format. "
00245 "Use '-strict -1' to encode to this pixel format.\n",
00246 av_get_pix_fmt_name(s->streams[0]->codec->pix_fmt));
00247 return AVERROR(EINVAL);
00248 }
00249 av_log(s, AV_LOG_WARNING, "Warning: generating non standart YUV stream. "
00250 "Mjpegtools will not work.\n");
00251 break;
00252 default:
00253 av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
00254 "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
00255 "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
00256 "yuv444p10, yuv422p10, yuv420p10, "
00257 "yuv444p16, yuv422p16, yuv420p16 "
00258 "and gray16 pixel formats. "
00259 "Use -pix_fmt to select one.\n");
00260 return AVERROR(EIO);
00261 }
00262
00263 *first_pkt = 1;
00264 return 0;
00265 }
00266
00267 AVOutputFormat ff_yuv4mpegpipe_muxer = {
00268 .name = "yuv4mpegpipe",
00269 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
00270 .extensions = "y4m",
00271 .priv_data_size = sizeof(int),
00272 .audio_codec = CODEC_ID_NONE,
00273 .video_codec = CODEC_ID_RAWVIDEO,
00274 .write_header = yuv4_write_header,
00275 .write_packet = yuv4_write_packet,
00276 .flags = AVFMT_RAWPICTURE,
00277 };
00278 #endif
00279
00280
00281 #define MAX_YUV4_HEADER 80
00282 #define MAX_FRAME_HEADER 80
00283
00284 static int yuv4_read_header(AVFormatContext *s)
00285 {
00286 char header[MAX_YUV4_HEADER + 10];
00287
00288 char *tokstart, *tokend, *header_end;
00289 int i;
00290 AVIOContext *pb = s->pb;
00291 int width = -1, height = -1, raten = 0,
00292 rated = 0, aspectn = 0, aspectd = 0;
00293 enum PixelFormat pix_fmt = PIX_FMT_NONE, alt_pix_fmt = PIX_FMT_NONE;
00294 enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
00295 AVStream *st;
00296 struct frame_attributes *s1 = s->priv_data;
00297
00298 for (i = 0; i < MAX_YUV4_HEADER; i++) {
00299 header[i] = avio_r8(pb);
00300 if (header[i] == '\n') {
00301 header[i + 1] = 0x20;
00302
00303 header[i + 2] = 0;
00304 break;
00305 }
00306 }
00307 if (i == MAX_YUV4_HEADER)
00308 return -1;
00309 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
00310 return -1;
00311
00312 s1->interlaced_frame = 0;
00313 s1->top_field_first = 0;
00314 header_end = &header[i + 1];
00315 for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
00316 tokstart < header_end; tokstart++) {
00317 if (*tokstart == 0x20)
00318 continue;
00319 switch (*tokstart++) {
00320 case 'W':
00321 width = strtol(tokstart, &tokend, 10);
00322 tokstart = tokend;
00323 break;
00324 case 'H':
00325 height = strtol(tokstart, &tokend, 10);
00326 tokstart = tokend;
00327 break;
00328 case 'C':
00329 if (strncmp("420jpeg", tokstart, 7) == 0) {
00330 pix_fmt = PIX_FMT_YUV420P;
00331 chroma_sample_location = AVCHROMA_LOC_CENTER;
00332 } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
00333 pix_fmt = PIX_FMT_YUV420P;
00334 chroma_sample_location = AVCHROMA_LOC_LEFT;
00335 } else if (strncmp("420paldv", tokstart, 8) == 0) {
00336 pix_fmt = PIX_FMT_YUV420P;
00337 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
00338 } else if (strncmp("420p16", tokstart, 6) == 0) {
00339 pix_fmt = PIX_FMT_YUV420P16;
00340 } else if (strncmp("422p16", tokstart, 6) == 0) {
00341 pix_fmt = PIX_FMT_YUV422P16;
00342 } else if (strncmp("444p16", tokstart, 6) == 0) {
00343 pix_fmt = PIX_FMT_YUV444P16;
00344 } else if (strncmp("420p10", tokstart, 6) == 0) {
00345 pix_fmt = PIX_FMT_YUV420P10;
00346 } else if (strncmp("422p10", tokstart, 6) == 0) {
00347 pix_fmt = PIX_FMT_YUV422P10;
00348 } else if (strncmp("444p10", tokstart, 6) == 0) {
00349 pix_fmt = PIX_FMT_YUV444P10;
00350 } else if (strncmp("420p9", tokstart, 5) == 0) {
00351 pix_fmt = PIX_FMT_YUV420P9;
00352 } else if (strncmp("422p9", tokstart, 5) == 0) {
00353 pix_fmt = PIX_FMT_YUV422P9;
00354 } else if (strncmp("444p9", tokstart, 5) == 0) {
00355 pix_fmt = PIX_FMT_YUV444P9;
00356 } else if (strncmp("420", tokstart, 3) == 0) {
00357 pix_fmt = PIX_FMT_YUV420P;
00358 chroma_sample_location = AVCHROMA_LOC_CENTER;
00359 } else if (strncmp("411", tokstart, 3) == 0) {
00360 pix_fmt = PIX_FMT_YUV411P;
00361 } else if (strncmp("422", tokstart, 3) == 0) {
00362 pix_fmt = PIX_FMT_YUV422P;
00363 } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
00364 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
00365 "YUV4MPEG stream.\n");
00366 return -1;
00367 } else if (strncmp("444", tokstart, 3) == 0) {
00368 pix_fmt = PIX_FMT_YUV444P;
00369 } else if (strncmp("mono", tokstart, 4) == 0) {
00370 pix_fmt = PIX_FMT_GRAY8;
00371 } else if (strncmp("mono16", tokstart, 6) == 0) {
00372 pix_fmt = PIX_FMT_GRAY16;
00373 } else {
00374 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
00375 "pixel format.\n");
00376 return -1;
00377 }
00378 while (tokstart < header_end && *tokstart != 0x20)
00379 tokstart++;
00380 break;
00381 case 'I':
00382 switch (*tokstart++){
00383 case '?':
00384 break;
00385 case 'p':
00386 s1->interlaced_frame = 0;
00387 break;
00388 case 't':
00389 s1->interlaced_frame = 1;
00390 s1->top_field_first = 1;
00391 break;
00392 case 'b':
00393 s1->interlaced_frame = 1;
00394 s1->top_field_first = 0;
00395 break;
00396 case 'm':
00397 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
00398 "interlaced and non-interlaced frames.\n");
00399 return -1;
00400 default:
00401 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
00402 return -1;
00403 }
00404 break;
00405 case 'F':
00406 sscanf(tokstart, "%d:%d", &raten, &rated);
00407 while (tokstart < header_end && *tokstart != 0x20)
00408 tokstart++;
00409 break;
00410 case 'A':
00411 sscanf(tokstart, "%d:%d", &aspectn, &aspectd);
00412 while (tokstart < header_end && *tokstart != 0x20)
00413 tokstart++;
00414 break;
00415 case 'X':
00416 if (strncmp("YSCSS=", tokstart, 6) == 0) {
00417
00418 tokstart += 6;
00419 if (strncmp("420JPEG", tokstart, 7) == 0)
00420 alt_pix_fmt = PIX_FMT_YUV420P;
00421 else if (strncmp("420MPEG2", tokstart, 8) == 0)
00422 alt_pix_fmt = PIX_FMT_YUV420P;
00423 else if (strncmp("420PALDV", tokstart, 8) == 0)
00424 alt_pix_fmt = PIX_FMT_YUV420P;
00425 else if (strncmp("420P9", tokstart, 5) == 0)
00426 alt_pix_fmt = PIX_FMT_YUV420P9;
00427 else if (strncmp("422P9", tokstart, 5) == 0)
00428 alt_pix_fmt = PIX_FMT_YUV422P9;
00429 else if (strncmp("444P9", tokstart, 5) == 0)
00430 alt_pix_fmt = PIX_FMT_YUV444P9;
00431 else if (strncmp("420P10", tokstart, 6) == 0)
00432 alt_pix_fmt = PIX_FMT_YUV420P10;
00433 else if (strncmp("422P10", tokstart, 6) == 0)
00434 alt_pix_fmt = PIX_FMT_YUV422P10;
00435 else if (strncmp("444P10", tokstart, 6) == 0)
00436 alt_pix_fmt = PIX_FMT_YUV444P10;
00437 else if (strncmp("420P16", tokstart, 6) == 0)
00438 alt_pix_fmt = PIX_FMT_YUV420P16;
00439 else if (strncmp("422P16", tokstart, 6) == 0)
00440 alt_pix_fmt = PIX_FMT_YUV422P16;
00441 else if (strncmp("444P16", tokstart, 6) == 0)
00442 alt_pix_fmt = PIX_FMT_YUV444P16;
00443 else if (strncmp("411", tokstart, 3) == 0)
00444 alt_pix_fmt = PIX_FMT_YUV411P;
00445 else if (strncmp("422", tokstart, 3) == 0)
00446 alt_pix_fmt = PIX_FMT_YUV422P;
00447 else if (strncmp("444", tokstart, 3) == 0)
00448 alt_pix_fmt = PIX_FMT_YUV444P;
00449 }
00450 while (tokstart < header_end && *tokstart != 0x20)
00451 tokstart++;
00452 break;
00453 }
00454 }
00455
00456 if (width == -1 || height == -1) {
00457 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
00458 return -1;
00459 }
00460
00461 if (pix_fmt == PIX_FMT_NONE) {
00462 if (alt_pix_fmt == PIX_FMT_NONE)
00463 pix_fmt = PIX_FMT_YUV420P;
00464 else
00465 pix_fmt = alt_pix_fmt;
00466 }
00467
00468 if (raten <= 0 || rated <= 0) {
00469
00470 raten = 25;
00471 rated = 1;
00472 }
00473
00474 if (aspectn == 0 && aspectd == 0) {
00475
00476 aspectd = 1;
00477 }
00478
00479 st = avformat_new_stream(s, NULL);
00480 if (!st)
00481 return AVERROR(ENOMEM);
00482 st->codec->width = width;
00483 st->codec->height = height;
00484 av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
00485 avpriv_set_pts_info(st, 64, rated, raten);
00486 st->codec->pix_fmt = pix_fmt;
00487 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00488 st->codec->codec_id = CODEC_ID_RAWVIDEO;
00489 st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
00490 st->codec->chroma_sample_location = chroma_sample_location;
00491
00492 return 0;
00493 }
00494
00495 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
00496 {
00497 int i;
00498 char header[MAX_FRAME_HEADER+1];
00499 int packet_size, width, height;
00500 AVStream *st = s->streams[0];
00501 struct frame_attributes *s1 = s->priv_data;
00502
00503 for (i = 0; i < MAX_FRAME_HEADER; i++) {
00504 header[i] = avio_r8(s->pb);
00505 if (header[i] == '\n') {
00506 header[i + 1] = 0;
00507 break;
00508 }
00509 }
00510 if (i == MAX_FRAME_HEADER)
00511 return -1;
00512 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
00513 return -1;
00514
00515 width = st->codec->width;
00516 height = st->codec->height;
00517
00518 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
00519 if (packet_size < 0)
00520 return -1;
00521
00522 if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
00523 return AVERROR(EIO);
00524
00525 if (st->codec->coded_frame) {
00526 st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
00527 st->codec->coded_frame->top_field_first = s1->top_field_first;
00528 }
00529
00530 pkt->stream_index = 0;
00531 return 0;
00532 }
00533
00534 static int yuv4_probe(AVProbeData *pd)
00535 {
00536
00537 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
00538 return AVPROBE_SCORE_MAX;
00539 else
00540 return 0;
00541 }
00542
00543 #if CONFIG_YUV4MPEGPIPE_DEMUXER
00544 AVInputFormat ff_yuv4mpegpipe_demuxer = {
00545 .name = "yuv4mpegpipe",
00546 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
00547 .priv_data_size = sizeof(struct frame_attributes),
00548 .read_probe = yuv4_probe,
00549 .read_header = yuv4_read_header,
00550 .read_packet = yuv4_read_packet,
00551 .extensions = "y4m",
00552 };
00553 #endif