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  int field_order;
37 
38  st = s->streams[0];
39  width = st->codecpar->width;
40  height = st->codecpar->height;
41  field_order = st->codecpar->field_order;
42 
43  // TODO: should be avg_frame_rate
44  av_reduce(&raten, &rated, st->time_base.den,
45  st->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 #if FF_API_LAVF_AVCTX
55  if (field_order != st->codec->field_order && st->codec->field_order != AV_FIELD_UNKNOWN)
56  field_order = st->codec->field_order;
58 #endif
59 
60  switch (field_order) {
61  case AV_FIELD_TB:
62  case AV_FIELD_TT: inter = 't'; break;
63  case AV_FIELD_BT:
64  case AV_FIELD_BB: inter = 'b'; break;
65  default: inter = 'p'; break;
66  }
67 
68  switch (st->codecpar->format) {
69  case AV_PIX_FMT_GRAY8:
70  colorspace = " Cmono";
71  break;
72  case AV_PIX_FMT_GRAY16:
73  colorspace = " Cmono16";
74  break;
75  case AV_PIX_FMT_YUV411P:
76  colorspace = " C411 XYSCSS=411";
77  break;
78  case AV_PIX_FMT_YUV420P:
79  switch (st->codecpar->chroma_location) {
80  case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
81  case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
82  default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
83  }
84  break;
85  case AV_PIX_FMT_YUV422P:
86  colorspace = " C422 XYSCSS=422";
87  break;
88  case AV_PIX_FMT_YUV444P:
89  colorspace = " C444 XYSCSS=444";
90  break;
92  colorspace = " C420p9 XYSCSS=420P9";
93  break;
95  colorspace = " C422p9 XYSCSS=422P9";
96  break;
98  colorspace = " C444p9 XYSCSS=444P9";
99  break;
101  colorspace = " C420p10 XYSCSS=420P10";
102  break;
104  colorspace = " C422p10 XYSCSS=422P10";
105  break;
107  colorspace = " C444p10 XYSCSS=444P10";
108  break;
110  colorspace = " C420p12 XYSCSS=420P12";
111  break;
113  colorspace = " C422p12 XYSCSS=422P12";
114  break;
116  colorspace = " C444p12 XYSCSS=444P12";
117  break;
119  colorspace = " C420p14 XYSCSS=420P14";
120  break;
122  colorspace = " C422p14 XYSCSS=422P14";
123  break;
125  colorspace = " C444p14 XYSCSS=444P14";
126  break;
128  colorspace = " C420p16 XYSCSS=420P16";
129  break;
131  colorspace = " C422p16 XYSCSS=422P16";
132  break;
134  colorspace = " C444p16 XYSCSS=444P16";
135  break;
136  }
137 
138  /* construct stream header, if this is the first frame */
139  n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
140  Y4M_MAGIC, width, height, raten, rated, inter,
141  aspectn, aspectd, colorspace);
142 
143  return n;
144 }
145 
147 {
148  AVStream *st = s->streams[pkt->stream_index];
149  AVIOContext *pb = s->pb;
150  AVFrame *frame;
151  int* first_pkt = s->priv_data;
152  int width, height, h_chroma_shift, v_chroma_shift;
153  int i;
154  char buf2[Y4M_LINE_MAX + 1];
155  uint8_t *ptr, *ptr1, *ptr2;
156 
157  frame = (AVFrame *)pkt->data;
158 
159  /* for the first packet we have to output the header as well */
160  if (*first_pkt) {
161  *first_pkt = 0;
162  if (yuv4_generate_header(s, buf2) < 0) {
163  av_log(s, AV_LOG_ERROR,
164  "Error. YUV4MPEG stream header write failed.\n");
165  return AVERROR(EIO);
166  } else {
167  avio_write(pb, buf2, strlen(buf2));
168  }
169  }
170 
171  /* construct frame header */
172 
173  avio_printf(s->pb, "%s\n", Y4M_FRAME_MAGIC);
174 
175  width = st->codecpar->width;
176  height = st->codecpar->height;
177 
178  ptr = frame->data[0];
179 
180  switch (st->codecpar->format) {
181  case AV_PIX_FMT_GRAY8:
182  case AV_PIX_FMT_YUV411P:
183  case AV_PIX_FMT_YUV420P:
184  case AV_PIX_FMT_YUV422P:
185  case AV_PIX_FMT_YUV444P:
186  break;
187  case AV_PIX_FMT_GRAY16:
188  case AV_PIX_FMT_YUV420P9:
189  case AV_PIX_FMT_YUV422P9:
190  case AV_PIX_FMT_YUV444P9:
203  width *= 2;
204  break;
205  default:
206  av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
208  return AVERROR(EINVAL);
209  }
210 
211  for (i = 0; i < height; i++) {
212  avio_write(pb, ptr, width);
213  ptr += frame->linesize[0];
214  }
215 
216  if (st->codecpar->format != AV_PIX_FMT_GRAY8 &&
218  // Adjust for smaller Cb and Cr planes
219  av_pix_fmt_get_chroma_sub_sample(st->codecpar->format, &h_chroma_shift,
220  &v_chroma_shift);
221  // Shift right, rounding up
222  width = AV_CEIL_RSHIFT(width, h_chroma_shift);
223  height = AV_CEIL_RSHIFT(height, v_chroma_shift);
224 
225  ptr1 = frame->data[1];
226  ptr2 = frame->data[2];
227  for (i = 0; i < height; i++) { /* Cb */
228  avio_write(pb, ptr1, width);
229  ptr1 += frame->linesize[1];
230  }
231  for (i = 0; i < height; i++) { /* Cr */
232  avio_write(pb, ptr2, width);
233  ptr2 += frame->linesize[2];
234  }
235  }
236 
237  return 0;
238 }
239 
241 {
242  int *first_pkt = s->priv_data;
243 
244  if (s->nb_streams != 1)
245  return AVERROR(EIO);
246 
248  av_log(s, AV_LOG_ERROR, "ERROR: Codec not supported.\n");
249  return AVERROR_INVALIDDATA;
250  }
251 
252  switch (s->streams[0]->codecpar->format) {
253  case AV_PIX_FMT_YUV411P:
254  av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
255  "stream, some mjpegtools might not work.\n");
256  break;
257  case AV_PIX_FMT_GRAY8:
258  case AV_PIX_FMT_GRAY16:
259  case AV_PIX_FMT_YUV420P:
260  case AV_PIX_FMT_YUV422P:
261  case AV_PIX_FMT_YUV444P:
262  break;
263  case AV_PIX_FMT_YUV420P9:
264  case AV_PIX_FMT_YUV422P9:
265  case AV_PIX_FMT_YUV444P9:
279  av_log(s, AV_LOG_ERROR, "'%s' is not an official yuv4mpegpipe pixel format. "
280  "Use '-strict -1' to encode to this pixel format.\n",
282  return AVERROR(EINVAL);
283  }
284  av_log(s, AV_LOG_WARNING, "Warning: generating non standard YUV stream. "
285  "Mjpegtools will not work.\n");
286  break;
287  default:
288  av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
289  "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
290  "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
291  "yuv444p10, yuv422p10, yuv420p10, "
292  "yuv444p12, yuv422p12, yuv420p12, "
293  "yuv444p14, yuv422p14, yuv420p14, "
294  "yuv444p16, yuv422p16, yuv420p16 "
295  "and gray16 pixel formats. "
296  "Use -pix_fmt to select one.\n");
297  return AVERROR(EIO);
298  }
299 
300  *first_pkt = 1;
301  return 0;
302 }
303 
305  .name = "yuv4mpegpipe",
306  .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
307  .extensions = "y4m",
308  .priv_data_size = sizeof(int),
309  .audio_codec = AV_CODEC_ID_NONE,
310  .video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
313 };
enum AVChromaLocation chroma_location
Definition: avcodec.h:4070
AVOutputFormat ff_yuv4mpegpipe_muxer
Definition: yuv4mpegenc.c:304
enum AVFieldOrder field_order
Video only.
Definition: avcodec.h:4061
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:481
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:351
static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: yuv4mpegenc.c:146
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3980
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:956
int num
Numerator.
Definition: rational.h:59
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:345
static AVPacket pkt
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1622
Format I/O context.
Definition: avformat.h:1338
uint8_t
int width
Video only.
Definition: avcodec.h:4046
#define Y4M_FRAME_MAGIC
Definition: yuv4mpeg.h:25
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1406
static AVFrame * frame
#define height
uint8_t * data
Definition: avcodec.h:1601
#define Y4M_LINE_MAX
Definition: yuv4mpegenc.c:27
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:354
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
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:346
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:204
#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:2294
#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:344
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1394
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:339
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:325
#define width
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:483
const char * name
Definition: avformat.h:524
int n
Definition: avisynth_c.h:684
Passthrough codec, AVFrames wrapped in AVPacket.
Definition: avcodec.h:652
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:340
Stream structure.
Definition: avformat.h:889
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:352
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:349
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
AVIOContext * pb
I/O context.
Definition: avformat.h:1380
void * buf
Definition: avisynth_c.h:690
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:341
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: ffmpeg.c:645
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2897
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:338
#define snprintf
Definition: snprintf.h:34
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:350
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:342
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:348
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
Main libavformat public API header.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Y , 8bpp.
Definition: pixfmt.h:70
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
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:69
int den
Denominator.
Definition: rational.h:60
static int yuv4_generate_header(AVFormatContext *s, char *buf)
Definition: yuv4mpegenc.c:29
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
void * priv_data
Format private data.
Definition: avformat.h:1366
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:344
AVCodecParameters * codecpar
Definition: avformat.h:1241
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:2182
int stream_index
Definition: avcodec.h:1603
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:926
static int yuv4_write_header(AVFormatContext *s)
Definition: yuv4mpegenc.c:240
This structure stores compressed data.
Definition: avcodec.h:1578
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:353
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58