FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rawdec.c
Go to the documentation of this file.
1 /*
2  * Raw Video Decoder
3  * Copyright (c) 2001 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 /**
23  * @file
24  * Raw Video Decoder
25  */
26 
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "raw.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/buffer.h"
32 #include "libavutil/common.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/opt.h"
36 
37 typedef struct RawVideoContext {
40  int frame_size; /* size of the frame in bytes */
41  int flip;
42  int is_2_4_bpp; // 2 or 4 bpp raw in avi/mov
43  int is_yuv2;
44  int is_lt_16bpp; // 16bpp pixfmt and bits_per_coded_sample < 16
45  int tff;
47 
48 static const AVOption options[]={
49 {"top", "top field first", offsetof(RawVideoContext, tff), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM},
50 {NULL}
51 };
52 
53 static const AVClass rawdec_class = {
54  .class_name = "rawdec",
55  .option = options,
56  .version = LIBAVUTIL_VERSION_INT,
57 };
58 
59 static const PixelFormatTag pix_fmt_bps_avi[] = {
60  { AV_PIX_FMT_MONOWHITE, 1 },
61  { AV_PIX_FMT_PAL8, 2 },
62  { AV_PIX_FMT_PAL8, 4 },
63  { AV_PIX_FMT_PAL8, 8 },
64  { AV_PIX_FMT_RGB444LE, 12 },
65  { AV_PIX_FMT_RGB555LE, 15 },
66  { AV_PIX_FMT_RGB555LE, 16 },
67  { AV_PIX_FMT_BGR24, 24 },
68  { AV_PIX_FMT_BGRA, 32 },
69  { AV_PIX_FMT_NONE, 0 },
70 };
71 
72 static const PixelFormatTag pix_fmt_bps_mov[] = {
73  { AV_PIX_FMT_MONOWHITE, 1 },
74  { AV_PIX_FMT_PAL8, 2 },
75  { AV_PIX_FMT_PAL8, 4 },
76  { AV_PIX_FMT_PAL8, 8 },
77  // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
78  // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
79  { AV_PIX_FMT_RGB555BE, 16 },
80  { AV_PIX_FMT_RGB24, 24 },
81  { AV_PIX_FMT_ARGB, 32 },
82  { AV_PIX_FMT_MONOWHITE,33 },
83  { AV_PIX_FMT_NONE, 0 },
84 };
85 
86 #if LIBAVCODEC_VERSION_MAJOR < 55
87 enum AVPixelFormat ff_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
88 {
89  return avpriv_find_pix_fmt(tags, fourcc);
90 }
91 #endif
92 
94 {
95  RawVideoContext *context = avctx->priv_data;
96  const AVPixFmtDescriptor *desc;
97 
98  if ( avctx->codec_tag == MKTAG('r','a','w',' ')
99  || avctx->codec_tag == MKTAG('N','O','1','6'))
100  avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_mov,
101  avctx->bits_per_coded_sample);
102  else if (avctx->codec_tag == MKTAG('W', 'R', 'A', 'W'))
103  avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_avi,
104  avctx->bits_per_coded_sample);
105  else if (avctx->codec_tag)
107  else if (avctx->pix_fmt == AV_PIX_FMT_NONE && avctx->bits_per_coded_sample)
108  avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_avi,
109  avctx->bits_per_coded_sample);
110 
111  desc = av_pix_fmt_desc_get(avctx->pix_fmt);
112  if (!desc) {
113  av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
114  return AVERROR(EINVAL);
115  }
116 
119  if (!context->palette)
120  return AVERROR(ENOMEM);
121  if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
122  avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
123  else
124  memset(context->palette->data, 0, AVPALETTE_SIZE);
125  }
126 
127  if ((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
128  avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
129  (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))) {
130  context->is_2_4_bpp = 1;
131  context->frame_size = avpicture_get_size(avctx->pix_fmt,
132  FFALIGN(avctx->width, 16),
133  avctx->height);
134  } else {
135  context->is_lt_16bpp = av_get_bits_per_pixel(desc) == 16 && avctx->bits_per_coded_sample && avctx->bits_per_coded_sample < 16;
136  context->frame_size = avpicture_get_size(avctx->pix_fmt, avctx->width,
137  avctx->height);
138  }
139 
140  if ((avctx->extradata_size >= 9 &&
141  !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
142  avctx->codec_tag == MKTAG('c','y','u','v') ||
143  avctx->codec_tag == MKTAG(3, 0, 0, 0) ||
144  avctx->codec_tag == MKTAG('W','R','A','W'))
145  context->flip = 1;
146 
147  if (avctx->codec_tag == AV_RL32("yuv2") &&
148  avctx->pix_fmt == AV_PIX_FMT_YUYV422)
149  context->is_yuv2 = 1;
150 
151  return 0;
152 }
153 
154 static void flip(AVCodecContext *avctx, AVPicture *picture)
155 {
156  picture->data[0] += picture->linesize[0] * (avctx->height - 1);
157  picture->linesize[0] *= -1;
158 }
159 
160 static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
161  AVPacket *avpkt)
162 {
163  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
164  RawVideoContext *context = avctx->priv_data;
165  const uint8_t *buf = avpkt->data;
166  int buf_size = avpkt->size;
167  int linesize_align = 4;
168  int res, len;
169  int need_copy = !avpkt->buf || context->is_2_4_bpp || context->is_yuv2 || context->is_lt_16bpp;
170 
171  AVFrame *frame = data;
172  AVPicture *picture = data;
173 
174  frame->pict_type = AV_PICTURE_TYPE_I;
175  frame->key_frame = 1;
176  frame->reordered_opaque = avctx->reordered_opaque;
177  frame->pkt_pts = avctx->internal->pkt->pts;
178  av_frame_set_pkt_pos (frame, avctx->internal->pkt->pos);
180 
181  if (context->tff >= 0) {
182  frame->interlaced_frame = 1;
183  frame->top_field_first = context->tff;
184  }
185 
186  if ((res = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
187  return res;
188 
189  if (need_copy)
190  frame->buf[0] = av_buffer_alloc(FFMAX(context->frame_size, buf_size));
191  else
192  frame->buf[0] = av_buffer_ref(avpkt->buf);
193  if (!frame->buf[0])
194  return AVERROR(ENOMEM);
195 
196  //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
197  if (context->is_2_4_bpp) {
198  int i;
199  uint8_t *dst = frame->buf[0]->data;
200  buf_size = context->frame_size - AVPALETTE_SIZE;
201  if (avctx->bits_per_coded_sample == 4) {
202  for (i = 0; 2 * i + 1 < buf_size && i<avpkt->size; i++) {
203  dst[2 * i + 0] = buf[i] >> 4;
204  dst[2 * i + 1] = buf[i] & 15;
205  }
206  linesize_align = 8;
207  } else {
208  av_assert0(avctx->bits_per_coded_sample == 2);
209  for (i = 0; 4 * i + 3 < buf_size && i<avpkt->size; i++) {
210  dst[4 * i + 0] = buf[i] >> 6;
211  dst[4 * i + 1] = buf[i] >> 4 & 3;
212  dst[4 * i + 2] = buf[i] >> 2 & 3;
213  dst[4 * i + 3] = buf[i] & 3;
214  }
215  linesize_align = 16;
216  }
217  buf = dst;
218  } else if (context->is_lt_16bpp) {
219  int i;
220  uint8_t *dst = frame->buf[0]->data;
221  if (desc->flags & AV_PIX_FMT_FLAG_BE) {
222  for (i = 0; i + 1 < buf_size; i += 2)
223  AV_WB16(dst + i, AV_RB16(buf + i) << (16 - avctx->bits_per_coded_sample));
224  } else {
225  for (i = 0; i + 1 < buf_size; i += 2)
226  AV_WL16(dst + i, AV_RL16(buf + i) << (16 - avctx->bits_per_coded_sample));
227  }
228  buf = dst;
229  } else if (need_copy) {
230  memcpy(frame->buf[0]->data, buf, buf_size);
231  buf = frame->buf[0]->data;
232  }
233 
234  if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
235  avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
236  buf += buf_size - context->frame_size;
237 
238  len = context->frame_size - (avctx->pix_fmt==AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
239  if (buf_size < len) {
240  av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected frame_size %d\n", buf_size, len);
241  av_buffer_unref(&frame->buf[0]);
242  return AVERROR(EINVAL);
243  }
244 
245  if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
246  avctx->width, avctx->height)) < 0) {
247  av_buffer_unref(&frame->buf[0]);
248  return res;
249  }
250 
251  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
253  NULL);
254 
255  if (pal) {
256  av_buffer_unref(&context->palette);
258  if (!context->palette) {
259  av_buffer_unref(&frame->buf[0]);
260  return AVERROR(ENOMEM);
261  }
262  memcpy(context->palette->data, pal, AVPALETTE_SIZE);
263  frame->palette_has_changed = 1;
264  }
265  }
266 
267  if ((avctx->pix_fmt==AV_PIX_FMT_BGR24 ||
268  avctx->pix_fmt==AV_PIX_FMT_GRAY8 ||
269  avctx->pix_fmt==AV_PIX_FMT_RGB555LE ||
270  avctx->pix_fmt==AV_PIX_FMT_RGB555BE ||
271  avctx->pix_fmt==AV_PIX_FMT_RGB565LE ||
272  avctx->pix_fmt==AV_PIX_FMT_MONOWHITE ||
273  avctx->pix_fmt==AV_PIX_FMT_PAL8) &&
274  FFALIGN(frame->linesize[0], linesize_align) * avctx->height <= buf_size)
275  frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
276 
277  if (avctx->pix_fmt == AV_PIX_FMT_NV12 && avctx->codec_tag == MKTAG('N', 'V', '1', '2') &&
278  FFALIGN(frame->linesize[0], linesize_align) * avctx->height +
279  FFALIGN(frame->linesize[1], linesize_align) * ((avctx->height + 1) / 2) <= buf_size) {
280  int la0 = FFALIGN(frame->linesize[0], linesize_align);
281  frame->data[1] += (la0 - frame->linesize[0]) * avctx->height;
282  frame->linesize[0] = la0;
283  frame->linesize[1] = FFALIGN(frame->linesize[1], linesize_align);
284  }
285 
286  if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->frame_size) ||
287  (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)) {
288  frame->buf[1] = av_buffer_ref(context->palette);
289  if (!frame->buf[1]) {
290  av_buffer_unref(&frame->buf[0]);
291  return AVERROR(ENOMEM);
292  }
293  frame->data[1] = frame->buf[1]->data;
294  }
295 
296  if (avctx->pix_fmt == AV_PIX_FMT_BGR24 &&
297  ((frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
298  frame->linesize[0] = (frame->linesize[0] + 3) & ~3;
299 
300  if (context->flip)
301  flip(avctx, picture);
302 
303  if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2') ||
304  avctx->codec_tag == MKTAG('Y', 'V', '1', '6') ||
305  avctx->codec_tag == MKTAG('Y', 'V', '2', '4') ||
306  avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
307  FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
308 
309  if (avctx->codec_tag == AV_RL32("I420") && (avctx->width+1)*(avctx->height+1) * 3/2 == buf_size) {
310  picture->data[1] = picture->data[1] + (avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height;
311  picture->data[2] = picture->data[2] + ((avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height)*5/4;
312  }
313 
314  if (avctx->codec_tag == AV_RL32("yuv2") &&
315  avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
316  int x, y;
317  uint8_t *line = picture->data[0];
318  for (y = 0; y < avctx->height; y++) {
319  for (x = 0; x < avctx->width; x++)
320  line[2 * x + 1] ^= 0x80;
321  line += picture->linesize[0];
322  }
323  }
324  if (avctx->codec_tag == AV_RL32("YVYU") &&
325  avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
326  int x, y;
327  uint8_t *line = picture->data[0];
328  for(y = 0; y < avctx->height; y++) {
329  for(x = 0; x < avctx->width - 1; x += 2)
330  FFSWAP(uint8_t, line[2*x + 1], line[2*x + 3]);
331  line += picture->linesize[0];
332  }
333  }
334 
335  if (avctx->field_order > AV_FIELD_PROGRESSIVE) { /* we have interlaced material flagged in container */
336  frame->interlaced_frame = 1;
337  if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
338  frame->top_field_first = 1;
339  }
340 
341  *got_frame = 1;
342  return buf_size;
343 }
344 
346 {
347  RawVideoContext *context = avctx->priv_data;
348 
349  av_buffer_unref(&context->palette);
350  return 0;
351 }
352 
354  .name = "rawvideo",
355  .long_name = NULL_IF_CONFIG_SMALL("raw video"),
356  .type = AVMEDIA_TYPE_VIDEO,
357  .id = AV_CODEC_ID_RAWVIDEO,
358  .priv_data_size = sizeof(RawVideoContext),
361  .decode = raw_decode,
362  .priv_class = &rawdec_class,
363 };