FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
yuv4mpeg.c
Go to the documentation of this file.
1 /*
2  * YUV4MPEG format
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/pixdesc.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "libavutil/pixdesc.h"
26 
27 #define Y4M_MAGIC "YUV4MPEG2"
28 #define Y4M_FRAME_MAGIC "FRAME"
29 #define Y4M_LINE_MAX 256
30 
31 #if CONFIG_YUV4MPEGPIPE_MUXER
32 static int yuv4_generate_header(AVFormatContext *s, char* buf)
33 {
34  AVStream *st;
35  int width, height;
36  int raten, rated, aspectn, aspectd, n;
37  char inter;
38  const char *colorspace = "";
39 
40  st = s->streams[0];
41  width = st->codec->width;
42  height = st->codec->height;
43 
44  av_reduce(&raten, &rated, st->codec->time_base.den,
45  st->codec->time_base.num, (1UL << 31) - 1);
46 
47  aspectn = st->sample_aspect_ratio.num;
48  aspectd = st->sample_aspect_ratio.den;
49 
50  if (aspectn == 0 && aspectd == 1)
51  aspectd = 0; // 0:0 means unknown
52 
53  inter = 'p'; /* progressive is the default */
55  inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
57  inter = 'p';
58  } else if (st->codec->field_order == AV_FIELD_TB || st->codec->field_order == AV_FIELD_TT) {
59  inter = 't';
60  } else if (st->codec->field_order == AV_FIELD_BT || st->codec->field_order == AV_FIELD_BB) {
61  inter = 'b';
62  }
63 
64  switch (st->codec->pix_fmt) {
65  case AV_PIX_FMT_GRAY8:
66  colorspace = " Cmono";
67  break;
68  case AV_PIX_FMT_GRAY16:
69  colorspace = " Cmono16";
70  break;
71  case AV_PIX_FMT_YUV411P:
72  colorspace = " C411 XYSCSS=411";
73  break;
74  case AV_PIX_FMT_YUV420P:
75  switch (st->codec->chroma_sample_location) {
76  case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
77  case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
78  default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
79  }
80  break;
81  case AV_PIX_FMT_YUV422P:
82  colorspace = " C422 XYSCSS=422";
83  break;
84  case AV_PIX_FMT_YUV444P:
85  colorspace = " C444 XYSCSS=444";
86  break;
88  colorspace = " C420p9 XYSCSS=420P9";
89  break;
91  colorspace = " C422p9 XYSCSS=422P9";
92  break;
94  colorspace = " C444p9 XYSCSS=444P9";
95  break;
97  colorspace = " C420p10 XYSCSS=420P10";
98  break;
100  colorspace = " C422p10 XYSCSS=422P10";
101  break;
103  colorspace = " C444p10 XYSCSS=444P10";
104  break;
106  colorspace = " C420p12 XYSCSS=420P12";
107  break;
109  colorspace = " C422p12 XYSCSS=422P12";
110  break;
112  colorspace = " C444p12 XYSCSS=444P12";
113  break;
115  colorspace = " C420p14 XYSCSS=420P14";
116  break;
118  colorspace = " C422p14 XYSCSS=422P14";
119  break;
121  colorspace = " C444p14 XYSCSS=444P14";
122  break;
124  colorspace = " C420p16 XYSCSS=420P16";
125  break;
127  colorspace = " C422p16 XYSCSS=422P16";
128  break;
130  colorspace = " C444p16 XYSCSS=444P16";
131  break;
132  }
133 
134  /* construct stream header, if this is the first frame */
135  n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
136  Y4M_MAGIC, width, height, raten, rated, inter,
137  aspectn, aspectd, colorspace);
138 
139  return n;
140 }
141 
142 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
143 {
144  AVStream *st = s->streams[pkt->stream_index];
145  AVIOContext *pb = s->pb;
146  AVPicture *picture, picture_tmp;
147  int* first_pkt = s->priv_data;
148  int width, height, h_chroma_shift, v_chroma_shift;
149  int i;
150  char buf2[Y4M_LINE_MAX + 1];
151  char buf1[20];
152  uint8_t *ptr, *ptr1, *ptr2;
153 
154  memcpy(&picture_tmp, pkt->data, sizeof(AVPicture));
155  picture = &picture_tmp;
156 
157  /* for the first packet we have to output the header as well */
158  if (*first_pkt) {
159  *first_pkt = 0;
160  if (yuv4_generate_header(s, buf2) < 0) {
161  av_log(s, AV_LOG_ERROR,
162  "Error. YUV4MPEG stream header write failed.\n");
163  return AVERROR(EIO);
164  } else {
165  avio_write(pb, buf2, strlen(buf2));
166  }
167  }
168 
169  /* construct frame header */
170 
171  snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
172  avio_write(pb, buf1, strlen(buf1));
173 
174  width = st->codec->width;
175  height = st->codec->height;
176 
177  ptr = picture->data[0];
178 
179  switch (st->codec->pix_fmt) {
180  case AV_PIX_FMT_GRAY8:
181  case AV_PIX_FMT_YUV411P:
182  case AV_PIX_FMT_YUV420P:
183  case AV_PIX_FMT_YUV422P:
184  case AV_PIX_FMT_YUV444P:
185  break;
186  case AV_PIX_FMT_GRAY16:
187  case AV_PIX_FMT_YUV420P9:
188  case AV_PIX_FMT_YUV422P9:
189  case AV_PIX_FMT_YUV444P9:
202  width *= 2;
203  break;
204  default:
205  av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
207  return AVERROR(EINVAL);
208  }
209 
210  for (i = 0; i < height; i++) {
211  avio_write(pb, ptr, width);
212  ptr += picture->linesize[0];
213  }
214 
215  if (st->codec->pix_fmt != AV_PIX_FMT_GRAY8 &&
216  st->codec->pix_fmt != AV_PIX_FMT_GRAY16) {
217  // Adjust for smaller Cb and Cr planes
218  av_pix_fmt_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
219  &v_chroma_shift);
220  width = FF_CEIL_RSHIFT(width, h_chroma_shift);
221  height = FF_CEIL_RSHIFT(height, v_chroma_shift);
222 
223  ptr1 = picture->data[1];
224  ptr2 = picture->data[2];
225  for (i = 0; i < height; i++) { /* Cb */
226  avio_write(pb, ptr1, width);
227  ptr1 += picture->linesize[1];
228  }
229  for (i = 0; i < height; i++) { /* Cr */
230  avio_write(pb, ptr2, width);
231  ptr2 += picture->linesize[2];
232  }
233  }
234 
235  return 0;
236 }
237 
238 static int yuv4_write_header(AVFormatContext *s)
239 {
240  int *first_pkt = s->priv_data;
241 
242  if (s->nb_streams != 1)
243  return AVERROR(EIO);
244 
245  if (s->streams[0]->codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
246  av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
247  return AVERROR_INVALIDDATA;
248  }
249 
250  switch (s->streams[0]->codec->pix_fmt) {
251  case AV_PIX_FMT_YUV411P:
252  av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
253  "stream, some mjpegtools might not work.\n");
254  break;
255  case AV_PIX_FMT_GRAY8:
256  case AV_PIX_FMT_GRAY16:
257  case AV_PIX_FMT_YUV420P:
258  case AV_PIX_FMT_YUV422P:
259  case AV_PIX_FMT_YUV444P:
260  break;
261  case AV_PIX_FMT_YUV420P9:
262  case AV_PIX_FMT_YUV422P9:
263  case AV_PIX_FMT_YUV444P9:
277  av_log(s, AV_LOG_ERROR, "'%s' is not a official yuv4mpegpipe pixel format. "
278  "Use '-strict -1' to encode to this pixel format.\n",
280  return AVERROR(EINVAL);
281  }
282  av_log(s, AV_LOG_WARNING, "Warning: generating non standard YUV stream. "
283  "Mjpegtools will not work.\n");
284  break;
285  default:
286  av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
287  "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
288  "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
289  "yuv444p10, yuv422p10, yuv420p10, "
290  "yuv444p12, yuv422p12, yuv420p12, "
291  "yuv444p14, yuv422p14, yuv420p14, "
292  "yuv444p16, yuv422p16, yuv420p16 "
293  "and gray16 pixel formats. "
294  "Use -pix_fmt to select one.\n");
295  return AVERROR(EIO);
296  }
297 
298  *first_pkt = 1;
299  return 0;
300 }
301 
302 AVOutputFormat ff_yuv4mpegpipe_muxer = {
303  .name = "yuv4mpegpipe",
304  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
305  .extensions = "y4m",
306  .priv_data_size = sizeof(int),
307  .audio_codec = AV_CODEC_ID_NONE,
308  .video_codec = AV_CODEC_ID_RAWVIDEO,
309  .write_header = yuv4_write_header,
310  .write_packet = yuv4_write_packet,
312 };
313 #endif
314 
315 /* Header size increased to allow room for optional flags */
316 #define MAX_YUV4_HEADER 80
317 #define MAX_FRAME_HEADER 80
318 
320 {
321  char header[MAX_YUV4_HEADER + 10]; // Include headroom for
322  // the longest option
323  char *tokstart, *tokend, *header_end, interlaced = '?';
324  int i;
325  AVIOContext *pb = s->pb;
326  int width = -1, height = -1, raten = 0,
327  rated = 0, aspectn = 0, aspectd = 0;
329  enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
330  AVStream *st;
331 
332  for (i = 0; i < MAX_YUV4_HEADER; i++) {
333  header[i] = avio_r8(pb);
334  if (header[i] == '\n') {
335  header[i + 1] = 0x20; // Add a space after last option.
336  // Makes parsing "444" vs "444alpha" easier.
337  header[i + 2] = 0;
338  break;
339  }
340  }
341  if (i == MAX_YUV4_HEADER)
342  return -1;
343  if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
344  return -1;
345 
346  header_end = &header[i + 1]; // Include space
347  for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
348  tokstart < header_end; tokstart++) {
349  if (*tokstart == 0x20)
350  continue;
351  switch (*tokstart++) {
352  case 'W': // Width. Required.
353  width = strtol(tokstart, &tokend, 10);
354  tokstart = tokend;
355  break;
356  case 'H': // Height. Required.
357  height = strtol(tokstart, &tokend, 10);
358  tokstart = tokend;
359  break;
360  case 'C': // Color space
361  if (strncmp("420jpeg", tokstart, 7) == 0) {
362  pix_fmt = AV_PIX_FMT_YUV420P;
363  chroma_sample_location = AVCHROMA_LOC_CENTER;
364  } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
365  pix_fmt = AV_PIX_FMT_YUV420P;
366  chroma_sample_location = AVCHROMA_LOC_LEFT;
367  } else if (strncmp("420paldv", tokstart, 8) == 0) {
368  pix_fmt = AV_PIX_FMT_YUV420P;
369  chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
370  } else if (strncmp("420p16", tokstart, 6) == 0) {
371  pix_fmt = AV_PIX_FMT_YUV420P16;
372  } else if (strncmp("422p16", tokstart, 6) == 0) {
373  pix_fmt = AV_PIX_FMT_YUV422P16;
374  } else if (strncmp("444p16", tokstart, 6) == 0) {
375  pix_fmt = AV_PIX_FMT_YUV444P16;
376  } else if (strncmp("420p14", tokstart, 6) == 0) {
377  pix_fmt = AV_PIX_FMT_YUV420P14;
378  } else if (strncmp("422p14", tokstart, 6) == 0) {
379  pix_fmt = AV_PIX_FMT_YUV422P14;
380  } else if (strncmp("444p14", tokstart, 6) == 0) {
381  pix_fmt = AV_PIX_FMT_YUV444P14;
382  } else if (strncmp("420p12", tokstart, 6) == 0) {
383  pix_fmt = AV_PIX_FMT_YUV420P12;
384  } else if (strncmp("422p12", tokstart, 6) == 0) {
385  pix_fmt = AV_PIX_FMT_YUV422P12;
386  } else if (strncmp("444p12", tokstart, 6) == 0) {
387  pix_fmt = AV_PIX_FMT_YUV444P12;
388  } else if (strncmp("420p10", tokstart, 6) == 0) {
389  pix_fmt = AV_PIX_FMT_YUV420P10;
390  } else if (strncmp("422p10", tokstart, 6) == 0) {
391  pix_fmt = AV_PIX_FMT_YUV422P10;
392  } else if (strncmp("444p10", tokstart, 6) == 0) {
393  pix_fmt = AV_PIX_FMT_YUV444P10;
394  } else if (strncmp("420p9", tokstart, 5) == 0) {
395  pix_fmt = AV_PIX_FMT_YUV420P9;
396  } else if (strncmp("422p9", tokstart, 5) == 0) {
397  pix_fmt = AV_PIX_FMT_YUV422P9;
398  } else if (strncmp("444p9", tokstart, 5) == 0) {
399  pix_fmt = AV_PIX_FMT_YUV444P9;
400  } else if (strncmp("420", tokstart, 3) == 0) {
401  pix_fmt = AV_PIX_FMT_YUV420P;
402  chroma_sample_location = AVCHROMA_LOC_CENTER;
403  } else if (strncmp("411", tokstart, 3) == 0) {
404  pix_fmt = AV_PIX_FMT_YUV411P;
405  } else if (strncmp("422", tokstart, 3) == 0) {
406  pix_fmt = AV_PIX_FMT_YUV422P;
407  } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
408  av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
409  "YUV4MPEG stream.\n");
410  return -1;
411  } else if (strncmp("444", tokstart, 3) == 0) {
412  pix_fmt = AV_PIX_FMT_YUV444P;
413  } else if (strncmp("mono16", tokstart, 6) == 0) {
414  pix_fmt = AV_PIX_FMT_GRAY16;
415  } else if (strncmp("mono", tokstart, 4) == 0) {
416  pix_fmt = AV_PIX_FMT_GRAY8;
417  } else {
418  av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
419  "pixel format.\n");
420  return -1;
421  }
422  while (tokstart < header_end && *tokstart != 0x20)
423  tokstart++;
424  break;
425  case 'I': // Interlace type
426  interlaced = *tokstart++;
427  break;
428  case 'F': // Frame rate
429  sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
430  while (tokstart < header_end && *tokstart != 0x20)
431  tokstart++;
432  break;
433  case 'A': // Pixel aspect
434  sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
435  while (tokstart < header_end && *tokstart != 0x20)
436  tokstart++;
437  break;
438  case 'X': // Vendor extensions
439  if (strncmp("YSCSS=", tokstart, 6) == 0) {
440  // Older nonstandard pixel format representation
441  tokstart += 6;
442  if (strncmp("420JPEG", tokstart, 7) == 0)
443  alt_pix_fmt = AV_PIX_FMT_YUV420P;
444  else if (strncmp("420MPEG2", tokstart, 8) == 0)
445  alt_pix_fmt = AV_PIX_FMT_YUV420P;
446  else if (strncmp("420PALDV", tokstart, 8) == 0)
447  alt_pix_fmt = AV_PIX_FMT_YUV420P;
448  else if (strncmp("420P9", tokstart, 5) == 0)
449  alt_pix_fmt = AV_PIX_FMT_YUV420P9;
450  else if (strncmp("422P9", tokstart, 5) == 0)
451  alt_pix_fmt = AV_PIX_FMT_YUV422P9;
452  else if (strncmp("444P9", tokstart, 5) == 0)
453  alt_pix_fmt = AV_PIX_FMT_YUV444P9;
454  else if (strncmp("420P10", tokstart, 6) == 0)
455  alt_pix_fmt = AV_PIX_FMT_YUV420P10;
456  else if (strncmp("422P10", tokstart, 6) == 0)
457  alt_pix_fmt = AV_PIX_FMT_YUV422P10;
458  else if (strncmp("444P10", tokstart, 6) == 0)
459  alt_pix_fmt = AV_PIX_FMT_YUV444P10;
460  else if (strncmp("420P12", tokstart, 6) == 0)
461  alt_pix_fmt = AV_PIX_FMT_YUV420P12;
462  else if (strncmp("422P12", tokstart, 6) == 0)
463  alt_pix_fmt = AV_PIX_FMT_YUV422P12;
464  else if (strncmp("444P12", tokstart, 6) == 0)
465  alt_pix_fmt = AV_PIX_FMT_YUV444P12;
466  else if (strncmp("420P14", tokstart, 6) == 0)
467  alt_pix_fmt = AV_PIX_FMT_YUV420P14;
468  else if (strncmp("422P14", tokstart, 6) == 0)
469  alt_pix_fmt = AV_PIX_FMT_YUV422P14;
470  else if (strncmp("444P14", tokstart, 6) == 0)
471  alt_pix_fmt = AV_PIX_FMT_YUV444P14;
472  else if (strncmp("420P16", tokstart, 6) == 0)
473  alt_pix_fmt = AV_PIX_FMT_YUV420P16;
474  else if (strncmp("422P16", tokstart, 6) == 0)
475  alt_pix_fmt = AV_PIX_FMT_YUV422P16;
476  else if (strncmp("444P16", tokstart, 6) == 0)
477  alt_pix_fmt = AV_PIX_FMT_YUV444P16;
478  else if (strncmp("411", tokstart, 3) == 0)
479  alt_pix_fmt = AV_PIX_FMT_YUV411P;
480  else if (strncmp("422", tokstart, 3) == 0)
481  alt_pix_fmt = AV_PIX_FMT_YUV422P;
482  else if (strncmp("444", tokstart, 3) == 0)
483  alt_pix_fmt = AV_PIX_FMT_YUV444P;
484  }
485  while (tokstart < header_end && *tokstart != 0x20)
486  tokstart++;
487  break;
488  }
489  }
490 
491  if (width == -1 || height == -1) {
492  av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
493  return -1;
494  }
495 
496  if (pix_fmt == AV_PIX_FMT_NONE) {
497  if (alt_pix_fmt == AV_PIX_FMT_NONE)
498  pix_fmt = AV_PIX_FMT_YUV420P;
499  else
500  pix_fmt = alt_pix_fmt;
501  }
502 
503  if (raten <= 0 || rated <= 0) {
504  // Frame rate unknown
505  raten = 25;
506  rated = 1;
507  }
508 
509  if (aspectn == 0 && aspectd == 0) {
510  // Pixel aspect unknown
511  aspectd = 1;
512  }
513 
514  st = avformat_new_stream(s, NULL);
515  if (!st)
516  return AVERROR(ENOMEM);
517  st->codec->width = width;
518  st->codec->height = height;
519  av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
520  avpriv_set_pts_info(st, 64, rated, raten);
521  st->codec->pix_fmt = pix_fmt;
524  st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
525  st->codec->chroma_sample_location = chroma_sample_location;
526 
527  switch (interlaced){
528  case 'p':
530  break;
531  case 't':
533  break;
534  case 'b':
536  break;
537  case 'm':
538  av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
539  "interlaced and non-interlaced frames.\n");
540  case '?':
542  break;
543  default:
544  av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
545  return AVERROR(EINVAL);
546  }
547 
548  return 0;
549 }
550 
552 {
553  int i;
554  char header[MAX_FRAME_HEADER+1];
555  int packet_size, width, height, ret;
556  AVStream *st = s->streams[0];
557 
558  for (i = 0; i < MAX_FRAME_HEADER; i++) {
559  header[i] = avio_r8(s->pb);
560  if (header[i] == '\n') {
561  header[i + 1] = 0;
562  break;
563  }
564  }
565  if (s->pb->error)
566  return s->pb->error;
567  else if (s->pb->eof_reached)
568  return AVERROR_EOF;
569  else if (i == MAX_FRAME_HEADER)
570  return AVERROR_INVALIDDATA;
571 
572  if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
573  return AVERROR_INVALIDDATA;
574 
575  width = st->codec->width;
576  height = st->codec->height;
577 
578  packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
579  if (packet_size < 0)
580  return packet_size;
581 
582  ret = av_get_packet(s->pb, pkt, packet_size);
583  if (ret < 0)
584  return ret;
585  else if (ret != packet_size)
586  return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
587 
588  pkt->stream_index = 0;
589  return 0;
590 }
591 
592 static int yuv4_probe(AVProbeData *pd)
593 {
594  /* check file header */
595  if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
596  return AVPROBE_SCORE_MAX;
597  else
598  return 0;
599 }
600 
601 #if CONFIG_YUV4MPEGPIPE_DEMUXER
602 AVInputFormat ff_yuv4mpegpipe_demuxer = {
603  .name = "yuv4mpegpipe",
604  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
605  .read_probe = yuv4_probe,
606  .read_header = yuv4_read_header,
607  .read_packet = yuv4_read_packet,
608  .extensions = "y4m",
609 };
610 #endif