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