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