Libav
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 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 
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "raw.h"
30 #include "libavutil/buffer.h"
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/imgutils.h"
34 
35 typedef struct RawVideoContext {
37  int frame_size; /* size of the frame in bytes */
38  int flip;
39  int is_2_4_bpp; // 2 or 4 bpp raw in avi/mov
40  int is_yuv2;
42 
43 static const PixelFormatTag pix_fmt_bps_avi[] = {
44  { AV_PIX_FMT_PAL8, 4 },
45  { AV_PIX_FMT_PAL8, 8 },
46  { AV_PIX_FMT_RGB444, 12 },
47  { AV_PIX_FMT_RGB555, 15 },
48  { AV_PIX_FMT_RGB555, 16 },
49  { AV_PIX_FMT_BGR24, 24 },
50  { AV_PIX_FMT_RGB32, 32 },
51  { AV_PIX_FMT_NONE, 0 },
52 };
53 
54 static const PixelFormatTag pix_fmt_bps_mov[] = {
55  { AV_PIX_FMT_MONOWHITE, 1 },
56  { AV_PIX_FMT_PAL8, 2 },
57  { AV_PIX_FMT_PAL8, 4 },
58  { AV_PIX_FMT_PAL8, 8 },
59  // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
60  // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
61  { AV_PIX_FMT_RGB555BE, 16 },
62  { AV_PIX_FMT_RGB24, 24 },
63  { AV_PIX_FMT_ARGB, 32 },
64  { AV_PIX_FMT_MONOWHITE,33 },
65  { AV_PIX_FMT_NONE, 0 },
66 };
67 
68 static enum AVPixelFormat find_pix_fmt(const PixelFormatTag *tags,
69  unsigned int fourcc)
70 {
71  while (tags->pix_fmt >= 0) {
72  if (tags->fourcc == fourcc)
73  return tags->pix_fmt;
74  tags++;
75  }
76  return AV_PIX_FMT_YUV420P;
77 }
78 
80 {
81  RawVideoContext *context = avctx->priv_data;
82  const AVPixFmtDescriptor *desc;
83 
84  if (avctx->codec_tag == MKTAG('r', 'a', 'w', ' '))
85  avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_mov,
86  avctx->bits_per_coded_sample);
87  else if (avctx->codec_tag == MKTAG('W', 'R', 'A', 'W'))
88  avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi,
89  avctx->bits_per_coded_sample);
90  else if (avctx->codec_tag)
92  else if (avctx->pix_fmt == AV_PIX_FMT_NONE && avctx->bits_per_coded_sample)
93  avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi,
94  avctx->bits_per_coded_sample);
95 
96  desc = av_pix_fmt_desc_get(avctx->pix_fmt);
97  if (!desc) {
98  av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
99  return AVERROR(EINVAL);
100  }
101 
104  if (!context->palette)
105  return AVERROR(ENOMEM);
106  if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
107  avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
108  else
109  memset(context->palette->data, 0, AVPALETTE_SIZE);
110  }
111 
112  context->frame_size = avpicture_get_size(avctx->pix_fmt, avctx->width,
113  avctx->height);
114  if ((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
115  avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
116  (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' ')))
117  context->is_2_4_bpp = 1;
118 
119  if ((avctx->extradata_size >= 9 &&
120  !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
121  avctx->codec_tag == MKTAG(3, 0, 0, 0) ||
122  avctx->codec_tag == MKTAG('W','R','A','W'))
123  context->flip = 1;
124 
125  if (avctx->codec_tag == AV_RL32("yuv2") &&
126  avctx->pix_fmt == AV_PIX_FMT_YUYV422)
127  context->is_yuv2 = 1;
128 
129  return 0;
130 }
131 
132 static void flip(AVCodecContext *avctx, AVPicture *picture)
133 {
134  picture->data[0] += picture->linesize[0] * (avctx->height - 1);
135  picture->linesize[0] *= -1;
136 }
137 
138 static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
139  AVPacket *avpkt)
140 {
141  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
142  RawVideoContext *context = avctx->priv_data;
143  const uint8_t *buf = avpkt->data;
144  int buf_size = avpkt->size;
145  int need_copy = !avpkt->buf || context->is_2_4_bpp || context->is_yuv2;
146  int res;
147 
148  AVFrame *frame = data;
150 
151  frame->pict_type = AV_PICTURE_TYPE_I;
152  frame->key_frame = 1;
153  frame->reordered_opaque = avctx->reordered_opaque;
154  frame->pkt_pts = avctx->internal->pkt->pts;
155 
156  if (buf_size < context->frame_size - (avctx->pix_fmt == AV_PIX_FMT_PAL8 ?
157  AVPALETTE_SIZE : 0))
158  return -1;
159 
160  if (need_copy)
161  frame->buf[0] = av_buffer_alloc(context->frame_size);
162  else
163  frame->buf[0] = av_buffer_ref(avpkt->buf);
164  if (!frame->buf[0])
165  return AVERROR(ENOMEM);
166 
167  //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
168  if (context->is_2_4_bpp) {
169  int i;
170  uint8_t *dst = frame->buf[0]->data;
171  buf_size = context->frame_size - AVPALETTE_SIZE;
172  if (avctx->bits_per_coded_sample == 4) {
173  for (i = 0; 2 * i + 1 < buf_size; i++) {
174  dst[2 * i + 0] = buf[i] >> 4;
175  dst[2 * i + 1] = buf[i] & 15;
176  }
177  } else {
178  for (i = 0; 4 * i + 3 < buf_size; i++) {
179  dst[4 * i + 0] = buf[i] >> 6;
180  dst[4 * i + 1] = buf[i] >> 4 & 3;
181  dst[4 * i + 2] = buf[i] >> 2 & 3;
182  dst[4 * i + 3] = buf[i] & 3;
183  }
184  }
185  buf = dst;
186  } else if (need_copy) {
187  memcpy(frame->buf[0]->data, buf, FFMIN(buf_size, context->frame_size));
188  buf = frame->buf[0]->data;
189  }
190 
191  if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
192  avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
193  buf += buf_size - context->frame_size;
194 
195  if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
196  avctx->width, avctx->height)) < 0)
197  return res;
198 
199  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
201  NULL);
202 
203  if (pal) {
204  av_buffer_unref(&context->palette);
205  context->palette = av_buffer_alloc(AVPALETTE_SIZE);
206  if (!context->palette)
207  return AVERROR(ENOMEM);
208  memcpy(context->palette->data, pal, AVPALETTE_SIZE);
209  frame->palette_has_changed = 1;
210  }
211  }
212 
213  if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->frame_size) ||
214  (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)) {
215  frame->buf[1] = av_buffer_ref(context->palette);
216  if (!frame->buf[1])
217  return AVERROR(ENOMEM);
218  frame->data[1] = frame->buf[1]->data;
219  }
220  if (avctx->pix_fmt == AV_PIX_FMT_BGR24 &&
221  ((frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
222  frame->linesize[0] = (frame->linesize[0] + 3) & ~3;
223 
224  if (context->flip)
225  flip(avctx, picture);
226 
227  if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2') ||
228  avctx->codec_tag == MKTAG('Y', 'V', '1', '6') ||
229  avctx->codec_tag == MKTAG('Y', 'V', '2', '4') ||
230  avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
231  FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
232 
233  if (avctx->codec_tag == AV_RL32("yuv2") &&
234  avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
235  int x, y;
236  uint8_t *line = picture->data[0];
237  for (y = 0; y < avctx->height; y++) {
238  for (x = 0; x < avctx->width; x++)
239  line[2 * x + 1] ^= 0x80;
240  line += picture->linesize[0];
241  }
242  }
243 
244  *got_frame = 1;
245  return buf_size;
246 }
247 
249 {
250  RawVideoContext *context = avctx->priv_data;
251 
252  av_buffer_unref(&context->palette);
253  return 0;
254 }
255 
257  .name = "rawvideo",
258  .long_name = NULL_IF_CONFIG_SMALL("raw video"),
259  .type = AVMEDIA_TYPE_VIDEO,
260  .id = AV_CODEC_ID_RAWVIDEO,
261  .priv_data_size = sizeof(RawVideoContext),
264  .decode = raw_decode,
265 };