FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
yuv4mpegenc.c
Go to the documentation of this file.
1 /*
2  * YUV4MPEG muxer
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 "yuv4mpeg.h"
26 
27 #define Y4M_LINE_MAX 256
28 
30 {
31  AVStream *st;
32  int width, height;
33  int raten, rated, aspectn, aspectd, n;
34  char inter;
35  const char *colorspace = "";
36 
37  st = s->streams[0];
38  width = st->codec->width;
39  height = st->codec->height;
40 
41  // TODO: should be avg_frame_rate
42  av_reduce(&raten, &rated, st->time_base.den,
43  st->time_base.num, (1UL << 31) - 1);
44 
45  aspectn = st->sample_aspect_ratio.num;
46  aspectd = st->sample_aspect_ratio.den;
47 
48  if (aspectn == 0 && aspectd == 1)
49  aspectd = 0; // 0:0 means unknown
50 
51  switch (st->codec->field_order) {
52  case AV_FIELD_TB:
53  case AV_FIELD_TT: inter = 't'; break;
54  case AV_FIELD_BT:
55  case AV_FIELD_BB: inter = 'b'; break;
56  default: inter = 'p'; break;
57  }
58 
59  switch (st->codec->pix_fmt) {
60  case AV_PIX_FMT_GRAY8:
61  colorspace = " Cmono";
62  break;
63  case AV_PIX_FMT_GRAY16:
64  colorspace = " Cmono16";
65  break;
66  case AV_PIX_FMT_YUV411P:
67  colorspace = " C411 XYSCSS=411";
68  break;
69  case AV_PIX_FMT_YUV420P:
70  switch (st->codec->chroma_sample_location) {
71  case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
72  case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
73  default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
74  }
75  break;
76  case AV_PIX_FMT_YUV422P:
77  colorspace = " C422 XYSCSS=422";
78  break;
79  case AV_PIX_FMT_YUV444P:
80  colorspace = " C444 XYSCSS=444";
81  break;
83  colorspace = " C420p9 XYSCSS=420P9";
84  break;
86  colorspace = " C422p9 XYSCSS=422P9";
87  break;
89  colorspace = " C444p9 XYSCSS=444P9";
90  break;
92  colorspace = " C420p10 XYSCSS=420P10";
93  break;
95  colorspace = " C422p10 XYSCSS=422P10";
96  break;
98  colorspace = " C444p10 XYSCSS=444P10";
99  break;
101  colorspace = " C420p12 XYSCSS=420P12";
102  break;
104  colorspace = " C422p12 XYSCSS=422P12";
105  break;
107  colorspace = " C444p12 XYSCSS=444P12";
108  break;
110  colorspace = " C420p14 XYSCSS=420P14";
111  break;
113  colorspace = " C422p14 XYSCSS=422P14";
114  break;
116  colorspace = " C444p14 XYSCSS=444P14";
117  break;
119  colorspace = " C420p16 XYSCSS=420P16";
120  break;
122  colorspace = " C422p16 XYSCSS=422P16";
123  break;
125  colorspace = " C444p16 XYSCSS=444P16";
126  break;
127  }
128 
129  /* construct stream header, if this is the first frame */
130  n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
131  Y4M_MAGIC, width, height, raten, rated, inter,
132  aspectn, aspectd, colorspace);
133 
134  return n;
135 }
136 
138 {
139  AVStream *st = s->streams[pkt->stream_index];
140  AVIOContext *pb = s->pb;
141  AVFrame *frame;
142  int* first_pkt = s->priv_data;
143  int width, height, h_chroma_shift, v_chroma_shift;
144  int i;
145  char buf2[Y4M_LINE_MAX + 1];
146  uint8_t *ptr, *ptr1, *ptr2;
147 
148  frame = (AVFrame *)pkt->data;
149 
150  /* for the first packet we have to output the header as well */
151  if (*first_pkt) {
152  *first_pkt = 0;
153  if (yuv4_generate_header(s, buf2) < 0) {
154  av_log(s, AV_LOG_ERROR,
155  "Error. YUV4MPEG stream header write failed.\n");
156  return AVERROR(EIO);
157  } else {
158  avio_write(pb, buf2, strlen(buf2));
159  }
160  }
161 
162  /* construct frame header */
163 
164  avio_printf(s->pb, "%s\n", Y4M_FRAME_MAGIC);
165 
166  width = st->codec->width;
167  height = st->codec->height;
168 
169  ptr = frame->data[0];
170 
171  switch (st->codec->pix_fmt) {
172  case AV_PIX_FMT_GRAY8:
173  case AV_PIX_FMT_YUV411P:
174  case AV_PIX_FMT_YUV420P:
175  case AV_PIX_FMT_YUV422P:
176  case AV_PIX_FMT_YUV444P:
177  break;
178  case AV_PIX_FMT_GRAY16:
179  case AV_PIX_FMT_YUV420P9:
180  case AV_PIX_FMT_YUV422P9:
181  case AV_PIX_FMT_YUV444P9:
194  width *= 2;
195  break;
196  default:
197  av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
199  return AVERROR(EINVAL);
200  }
201 
202  for (i = 0; i < height; i++) {
203  avio_write(pb, ptr, width);
204  ptr += frame->linesize[0];
205  }
206 
207  if (st->codec->pix_fmt != AV_PIX_FMT_GRAY8 &&
208  st->codec->pix_fmt != AV_PIX_FMT_GRAY16) {
209  // Adjust for smaller Cb and Cr planes
210  av_pix_fmt_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
211  &v_chroma_shift);
212  width = AV_CEIL_RSHIFT(width, h_chroma_shift);
213  height = AV_CEIL_RSHIFT(height, v_chroma_shift);
214 
215  ptr1 = frame->data[1];
216  ptr2 = frame->data[2];
217  for (i = 0; i < height; i++) { /* Cb */
218  avio_write(pb, ptr1, width);
219  ptr1 += frame->linesize[1];
220  }
221  for (i = 0; i < height; i++) { /* Cr */
222  avio_write(pb, ptr2, width);
223  ptr2 += frame->linesize[2];
224  }
225  }
226 
227  return 0;
228 }
229 
231 {
232  int *first_pkt = s->priv_data;
233 
234  if (s->nb_streams != 1)
235  return AVERROR(EIO);
236 
238  av_log(s, AV_LOG_ERROR, "ERROR: Codec not supported.\n");
239  return AVERROR_INVALIDDATA;
240  }
241 
242  switch (s->streams[0]->codec->pix_fmt) {
243  case AV_PIX_FMT_YUV411P:
244  av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
245  "stream, some mjpegtools might not work.\n");
246  break;
247  case AV_PIX_FMT_GRAY8:
248  case AV_PIX_FMT_GRAY16:
249  case AV_PIX_FMT_YUV420P:
250  case AV_PIX_FMT_YUV422P:
251  case AV_PIX_FMT_YUV444P:
252  break;
253  case AV_PIX_FMT_YUV420P9:
254  case AV_PIX_FMT_YUV422P9:
255  case AV_PIX_FMT_YUV444P9:
269  av_log(s, AV_LOG_ERROR, "'%s' is not a official yuv4mpegpipe pixel format. "
270  "Use '-strict -1' to encode to this pixel format.\n",
272  return AVERROR(EINVAL);
273  }
274  av_log(s, AV_LOG_WARNING, "Warning: generating non standard YUV stream. "
275  "Mjpegtools will not work.\n");
276  break;
277  default:
278  av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
279  "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
280  "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
281  "yuv444p10, yuv422p10, yuv420p10, "
282  "yuv444p12, yuv422p12, yuv420p12, "
283  "yuv444p14, yuv422p14, yuv420p14, "
284  "yuv444p16, yuv422p16, yuv420p16 "
285  "and gray16 pixel formats. "
286  "Use -pix_fmt to select one.\n");
287  return AVERROR(EIO);
288  }
289 
290  *first_pkt = 1;
291  return 0;
292 }
293 
295  .name = "yuv4mpegpipe",
296  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
297  .extensions = "y4m",
298  .priv_data_size = sizeof(int),
299  .audio_codec = AV_CODEC_ID_NONE,
300  .video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
303 };
AVOutputFormat ff_yuv4mpegpipe_muxer
Definition: yuv4mpegenc.c:294
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
mpeg2/4 4:2:0, h264 default for 4:2:0
Definition: pixfmt.h:464
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:340
static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: yuv4mpegenc.c:137
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:68
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:949
int num
numerator
Definition: rational.h:44
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:334
static AVPacket pkt
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1596
Format I/O context.
Definition: avformat.h:1314
uint8_t
#define Y4M_FRAME_MAGIC
Definition: yuv4mpeg.h:25
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1467
#define Y4M_LINE_MAX
Definition: yuv4mpegenc.c:27
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:343
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:335
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:182
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2269
#define av_log(a,...)
#define Y4M_MAGIC
Definition: yuv4mpeg.h:24
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
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:2185
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:333
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
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 int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1370
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:328
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:314
int width
picture width / height.
Definition: avcodec.h:1711
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:466
const char * name
Definition: avformat.h:523
int n
Definition: avisynth_c.h:547
Passthrough codec, AVFrames wrapped in AVPacket.
Definition: avcodec.h:553
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:329
Stream structure.
Definition: avformat.h:877
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:341
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:338
enum AVCodecID codec_id
Definition: avcodec.h:1549
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
void * buf
Definition: avisynth_c.h:553
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:330
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2744
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:327
#define snprintf
Definition: snprintf.h:34
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:339
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:331
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:337
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
Main libavformat public API header.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
Y , 8bpp.
Definition: pixfmt.h:71
if(ret< 0)
Definition: vf_mcdeint.c:282
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:70
int den
denominator
Definition: rational.h:45
static int yuv4_generate_header(AVFormatContext *s, char *buf)
Definition: yuv4mpegenc.c:29
void * priv_data
Format private data.
Definition: avformat.h:1342
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:497
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:2284
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2078
int stream_index
Definition: avcodec.h:1469
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:919
static int yuv4_write_header(AVFormatContext *s)
Definition: yuv4mpegenc.c:230
This structure stores compressed data.
Definition: avcodec.h:1444
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:342
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
static int width