Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 
26 #define Y4M_MAGIC "YUV4MPEG2"
27 #define Y4M_FRAME_MAGIC "FRAME"
28 #define Y4M_LINE_MAX 256
29 
33 };
34 
35 #if CONFIG_YUV4MPEGPIPE_MUXER
36 static int yuv4_generate_header(AVFormatContext *s, char* buf)
37 {
38  AVStream *st;
39  int width, height;
40  int raten, rated, aspectn, aspectd, n;
41  char inter;
42  const char *colorspace = "";
43 
44  st = s->streams[0];
45  width = st->codec->width;
46  height = st->codec->height;
47 
48  av_reduce(&raten, &rated, st->codec->time_base.den,
49  st->codec->time_base.num, (1UL << 31) - 1);
50 
51  aspectn = st->sample_aspect_ratio.num;
52  aspectd = st->sample_aspect_ratio.den;
53 
54  if (aspectn == 0 && aspectd == 1)
55  aspectd = 0; // 0:0 means unknown
56 
57  inter = 'p'; /* progressive is the default */
59  inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
60 
61  switch (st->codec->pix_fmt) {
62  case AV_PIX_FMT_GRAY8:
63  colorspace = " Cmono";
64  break;
65  case AV_PIX_FMT_YUV411P:
66  colorspace = " C411 XYSCSS=411";
67  break;
68  case AV_PIX_FMT_YUV420P:
69  switch (st->codec->chroma_sample_location) {
70  case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
71  case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
72  default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
73  }
74  break;
75  case AV_PIX_FMT_YUV422P:
76  colorspace = " C422 XYSCSS=422";
77  break;
78  case AV_PIX_FMT_YUV444P:
79  colorspace = " C444 XYSCSS=444";
80  break;
81  }
82 
83  /* construct stream header, if this is the first frame */
84  n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
85  Y4M_MAGIC, width, height, raten, rated, inter,
86  aspectn, aspectd, colorspace);
87 
88  return n;
89 }
90 
91 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
92 {
93  AVStream *st = s->streams[pkt->stream_index];
94  AVIOContext *pb = s->pb;
96  int* first_pkt = s->priv_data;
97  int width, height, h_chroma_shift, v_chroma_shift;
98  int i;
99  char buf2[Y4M_LINE_MAX + 1];
100  char buf1[20];
101  uint8_t *ptr, *ptr1, *ptr2;
102 
103  picture = (AVPicture *)pkt->data;
104 
105  /* for the first packet we have to output the header as well */
106  if (*first_pkt) {
107  *first_pkt = 0;
108  if (yuv4_generate_header(s, buf2) < 0) {
109  av_log(s, AV_LOG_ERROR,
110  "Error. YUV4MPEG stream header write failed.\n");
111  return AVERROR(EIO);
112  } else {
113  avio_write(pb, buf2, strlen(buf2));
114  }
115  }
116 
117  /* construct frame header */
118 
119  snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
120  avio_write(pb, buf1, strlen(buf1));
121 
122  width = st->codec->width;
123  height = st->codec->height;
124 
125  ptr = picture->data[0];
126  for (i = 0; i < height; i++) {
127  avio_write(pb, ptr, width);
128  ptr += picture->linesize[0];
129  }
130 
131  if (st->codec->pix_fmt != AV_PIX_FMT_GRAY8) {
132  // Adjust for smaller Cb and Cr planes
133  av_pix_fmt_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
134  &v_chroma_shift);
135  // Shift right, rounding up
136  width = -(-width >> h_chroma_shift);
137  height = -(-height >> v_chroma_shift);
138 
139  ptr1 = picture->data[1];
140  ptr2 = picture->data[2];
141  for (i = 0; i < height; i++) { /* Cb */
142  avio_write(pb, ptr1, width);
143  ptr1 += picture->linesize[1];
144  }
145  for (i = 0; i < height; i++) { /* Cr */
146  avio_write(pb, ptr2, width);
147  ptr2 += picture->linesize[2];
148  }
149  }
150  return 0;
151 }
152 
153 static int yuv4_write_header(AVFormatContext *s)
154 {
155  int *first_pkt = s->priv_data;
156 
157  if (s->nb_streams != 1)
158  return AVERROR(EIO);
159 
160  if (s->streams[0]->codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
161  av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
162  return AVERROR_INVALIDDATA;
163  }
164 
165  if (s->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV411P) {
166  av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV "
167  "stream, some mjpegtools might not work.\n");
168  } else if ((s->streams[0]->codec->pix_fmt != AV_PIX_FMT_YUV420P) &&
169  (s->streams[0]->codec->pix_fmt != AV_PIX_FMT_YUV422P) &&
170  (s->streams[0]->codec->pix_fmt != AV_PIX_FMT_GRAY8) &&
171  (s->streams[0]->codec->pix_fmt != AV_PIX_FMT_YUV444P)) {
172  av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, "
173  "yuv422p, yuv420p, yuv411p and gray pixel formats. "
174  "Use -pix_fmt to select one.\n");
175  return AVERROR(EIO);
176  }
177 
178  *first_pkt = 1;
179  return 0;
180 }
181 
182 AVOutputFormat ff_yuv4mpegpipe_muxer = {
183  .name = "yuv4mpegpipe",
184  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
185  .mime_type = "",
186  .extensions = "y4m",
187  .priv_data_size = sizeof(int),
188  .audio_codec = AV_CODEC_ID_NONE,
189  .video_codec = AV_CODEC_ID_RAWVIDEO,
190  .write_header = yuv4_write_header,
191  .write_packet = yuv4_write_packet,
193 };
194 #endif
195 
196 /* Header size increased to allow room for optional flags */
197 #define MAX_YUV4_HEADER 80
198 #define MAX_FRAME_HEADER 80
199 
201 {
202  char header[MAX_YUV4_HEADER + 10]; // Include headroom for
203  // the longest option
204  char *tokstart, *tokend, *header_end;
205  int i;
206  AVIOContext *pb = s->pb;
207  int width = -1, height = -1, raten = 0,
208  rated = 0, aspectn = 0, aspectd = 0;
210  enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
211  AVStream *st;
212  struct frame_attributes *s1 = s->priv_data;
213 
214  for (i = 0; i < MAX_YUV4_HEADER; i++) {
215  header[i] = avio_r8(pb);
216  if (header[i] == '\n') {
217  header[i + 1] = 0x20; // Add a space after last option.
218  // Makes parsing "444" vs "444alpha" easier.
219  header[i + 2] = 0;
220  break;
221  }
222  }
223  if (i == MAX_YUV4_HEADER)
224  return -1;
225  if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
226  return -1;
227 
228  s1->interlaced_frame = 0;
229  s1->top_field_first = 0;
230  header_end = &header[i + 1]; // Include space
231  for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
232  tokstart < header_end; tokstart++) {
233  if (*tokstart == 0x20)
234  continue;
235  switch (*tokstart++) {
236  case 'W': // Width. Required.
237  width = strtol(tokstart, &tokend, 10);
238  tokstart = tokend;
239  break;
240  case 'H': // Height. Required.
241  height = strtol(tokstart, &tokend, 10);
242  tokstart = tokend;
243  break;
244  case 'C': // Color space
245  if (strncmp("420jpeg", tokstart, 7) == 0) {
246  pix_fmt = AV_PIX_FMT_YUV420P;
247  chroma_sample_location = AVCHROMA_LOC_CENTER;
248  } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
249  pix_fmt = AV_PIX_FMT_YUV420P;
250  chroma_sample_location = AVCHROMA_LOC_LEFT;
251  } else if (strncmp("420paldv", tokstart, 8) == 0) {
252  pix_fmt = AV_PIX_FMT_YUV420P;
253  chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
254  } else if (strncmp("420", tokstart, 3) == 0) {
255  pix_fmt = AV_PIX_FMT_YUV420P;
256  chroma_sample_location = AVCHROMA_LOC_CENTER;
257  } else if (strncmp("411", tokstart, 3) == 0)
258  pix_fmt = AV_PIX_FMT_YUV411P;
259  else if (strncmp("422", tokstart, 3) == 0)
260  pix_fmt = AV_PIX_FMT_YUV422P;
261  else if (strncmp("444alpha", tokstart, 8) == 0 ) {
262  av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
263  "YUV4MPEG stream.\n");
264  return -1;
265  } else if (strncmp("444", tokstart, 3) == 0)
266  pix_fmt = AV_PIX_FMT_YUV444P;
267  else if (strncmp("mono", tokstart, 4) == 0) {
268  pix_fmt = AV_PIX_FMT_GRAY8;
269  } else {
270  av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
271  "pixel format.\n");
272  return -1;
273  }
274  while (tokstart < header_end && *tokstart != 0x20)
275  tokstart++;
276  break;
277  case 'I': // Interlace type
278  switch (*tokstart++){
279  case '?':
280  break;
281  case 'p':
282  s1->interlaced_frame = 0;
283  break;
284  case 't':
285  s1->interlaced_frame = 1;
286  s1->top_field_first = 1;
287  break;
288  case 'b':
289  s1->interlaced_frame = 1;
290  s1->top_field_first = 0;
291  break;
292  case 'm':
293  av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
294  "interlaced and non-interlaced frames.\n");
295  return -1;
296  default:
297  av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
298  return -1;
299  }
300  break;
301  case 'F': // Frame rate
302  sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
303  while (tokstart < header_end && *tokstart != 0x20)
304  tokstart++;
305  break;
306  case 'A': // Pixel aspect
307  sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
308  while (tokstart < header_end && *tokstart != 0x20)
309  tokstart++;
310  break;
311  case 'X': // Vendor extensions
312  if (strncmp("YSCSS=", tokstart, 6) == 0) {
313  // Older nonstandard pixel format representation
314  tokstart += 6;
315  if (strncmp("420JPEG", tokstart, 7) == 0)
316  alt_pix_fmt = AV_PIX_FMT_YUV420P;
317  else if (strncmp("420MPEG2", tokstart, 8) == 0)
318  alt_pix_fmt = AV_PIX_FMT_YUV420P;
319  else if (strncmp("420PALDV", tokstart, 8) == 0)
320  alt_pix_fmt = AV_PIX_FMT_YUV420P;
321  else if (strncmp("411", tokstart, 3) == 0)
322  alt_pix_fmt = AV_PIX_FMT_YUV411P;
323  else if (strncmp("422", tokstart, 3) == 0)
324  alt_pix_fmt = AV_PIX_FMT_YUV422P;
325  else if (strncmp("444", tokstart, 3) == 0)
326  alt_pix_fmt = AV_PIX_FMT_YUV444P;
327  }
328  while (tokstart < header_end && *tokstart != 0x20)
329  tokstart++;
330  break;
331  }
332  }
333 
334  if (width == -1 || height == -1) {
335  av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
336  return -1;
337  }
338 
339  if (pix_fmt == AV_PIX_FMT_NONE) {
340  if (alt_pix_fmt == AV_PIX_FMT_NONE)
341  pix_fmt = AV_PIX_FMT_YUV420P;
342  else
343  pix_fmt = alt_pix_fmt;
344  }
345 
346  if (raten <= 0 || rated <= 0) {
347  // Frame rate unknown
348  raten = 25;
349  rated = 1;
350  }
351 
352  if (aspectn == 0 && aspectd == 0) {
353  // Pixel aspect unknown
354  aspectd = 1;
355  }
356 
357  st = avformat_new_stream(s, NULL);
358  if (!st)
359  return AVERROR(ENOMEM);
360  st->codec->width = width;
361  st->codec->height = height;
362  av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
363  avpriv_set_pts_info(st, 64, rated, raten);
364  st->avg_frame_rate = av_inv_q(st->time_base);
365  st->codec->pix_fmt = pix_fmt;
368  st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
369  st->codec->chroma_sample_location = chroma_sample_location;
370 
371  return 0;
372 }
373 
375 {
376  int i;
377  char header[MAX_FRAME_HEADER+1];
378  int packet_size, width, height, ret;
379  AVStream *st = s->streams[0];
380  struct frame_attributes *s1 = s->priv_data;
381 
382  for (i = 0; i < MAX_FRAME_HEADER; i++) {
383  header[i] = avio_r8(s->pb);
384  if (header[i] == '\n') {
385  header[i + 1] = 0;
386  break;
387  }
388  }
389  if (s->pb->error)
390  return s->pb->error;
391  else if (s->pb->eof_reached)
392  return AVERROR_EOF;
393  else if (i == MAX_FRAME_HEADER)
394  return AVERROR_INVALIDDATA;
395 
396  if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
397  return AVERROR_INVALIDDATA;
398 
399  width = st->codec->width;
400  height = st->codec->height;
401 
402  packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
403  if (packet_size < 0)
404  return packet_size;
405 
406  ret = av_get_packet(s->pb, pkt, packet_size);
407  if (ret < 0)
408  return ret;
409  else if (ret != packet_size)
410  return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
411 
412  if (st->codec->coded_frame) {
415  }
416 
417  pkt->stream_index = 0;
418  return 0;
419 }
420 
421 static int yuv4_probe(AVProbeData *pd)
422 {
423  /* check file header */
424  if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
425  return AVPROBE_SCORE_MAX;
426  else
427  return 0;
428 }
429 
430 #if CONFIG_YUV4MPEGPIPE_DEMUXER
431 AVInputFormat ff_yuv4mpegpipe_demuxer = {
432  .name = "yuv4mpegpipe",
433  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
434  .priv_data_size = sizeof(struct frame_attributes),
436  .read_header = yuv4_read_header,
437  .read_packet = yuv4_read_packet,
438  .extensions = "y4m",
439 };
440 #endif
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define MAX_FRAME_HEADER
Definition: yuv4mpeg.c:198
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
Definition: avcodec.h:2952
mpeg2/4, h264 default
Definition: avcodec.h:607
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:3208
static AVFrame * picture
Definition: output.c:179
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2506
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:747
int num
numerator
Definition: rational.h:44
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
AVChromaLocation
X X 3 4 X X are luma samples, 1 2 1-6 are possible chroma positions X X 5 6 X 0 is undefined/unknown ...
Definition: avcodec.h:605
four components are given, that's all.
Definition: avcodec.h:2950
static int yuv4_probe(AVProbeData *pd)
Definition: yuv4mpeg.c:421
#define Y4M_LINE_MAX
Definition: yuv4mpeg.c:28
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1173
Format I/O context.
Definition: avformat.h:871
uint8_t
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition: avcodec.h:2951
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:935
mpeg1, jpeg, h263
Definition: avcodec.h:608
#define Y4M_MAGIC
Definition: yuv4mpeg.c:26
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
#define AVERROR_EOF
End of file.
Definition: error.h:51
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:118
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:292
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1761
#define MAX_YUV4_HEADER
Definition: yuv4mpeg.c:197
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2662
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:754
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:437
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:702
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:390
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:923
#define Y4M_FRAME_MAGIC
Definition: yuv4mpeg.c:27
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1217
const char * name
Definition: avformat.h:437
enum AVPixelFormat pix_fmt
Definition: movenc.c:821
if(ac->has_optimized_func)
Stream structure.
Definition: avformat.h:683
int interlaced_frame
Definition: yuv4mpeg.c:31
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
enum AVMediaType codec_type
Definition: avcodec.h:1062
enum AVCodecID codec_id
Definition: avcodec.h:1065
AVIOContext * pb
I/O context.
Definition: avformat.h:913
static int yuv4_read_header(AVFormatContext *s)
Definition: yuv4mpeg.c:200
#define AVFMT_RAWPICTURE
Format wants AVPicture structure for raw picture data.
Definition: avformat.h:403
rational number numerator/denominator
Definition: rational.h:43
int error
contains the error code or 0 if no error happened
Definition: avio.h:102
This structure contains the data a format has to probe a file.
Definition: avformat.h:388
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:122
int height
Definition: gxfenc.c:72
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:395
Main libavformat public API header.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
Y , 8bpp.
Definition: pixfmt.h:73
FF_ENABLE_DEPRECATION_WARNINGS int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:1533
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
int den
denominator
Definition: rational.h:45
int eof_reached
true if eof reached
Definition: avio.h:96
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:297
void * priv_data
Format private data.
Definition: avformat.h:899
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: avpicture.c:85
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:516
static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: yuv4mpeg.c:374
enum AVColorSpace colorspace
Definition: dirac.c:100
int stream_index
Definition: avcodec.h:975
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:719
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950
int top_field_first
Definition: yuv4mpeg.c:32