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
39
51 int is_lt_16bpp;
// 16bpp pixfmt and bits_per_coded_sample < 16
53
58
62 };
63
68 };
69
71 {
74
76
89
94 }
95
102 memset(
context->palette->data, 0xff, 4);
103 }
104
111
117
123
127
128 return 0;
129 }
130
132 {
134 frame->linesize[0] *= -1;
135 }
136
137 /*
138 * Scale sample to 16-bit resolution
139 */
140 #define SCALE16(x, bits) (((x) << (16 - (bits))) | ((x) >> (2 * (bits) - 16)))
141
142 /**
143 * Scale buffer to 16 bits per coded sample resolution
144 */
145 #define MKSCALE16(name, r16, w16) \
146 static void name(AVCodecContext *avctx, uint8_t * dst, const uint8_t *buf, int buf_size, int packed) \
147 { \
148 int i; \
149 if (!packed) { \
150 for (i = 0; i + 1 < buf_size; i += 2) \
151 w16(dst + i, SCALE16(r16(buf + i), avctx->bits_per_coded_sample)); \
152 } else { \
153 GetBitContext gb; \
154 init_get_bits(&gb, buf, buf_size * 8); \
155 for (i = 0; i < avctx->width * avctx->height; i++) { \
156 int sample = get_bits(&gb, avctx->bits_per_coded_sample); \
157 w16(dst + i*2, SCALE16(sample, avctx->bits_per_coded_sample)); \
158 } \
159 } \
160 }
161
164
167 {
170 const uint8_t *buf = avpkt->data;
171 int buf_size = avpkt->size;
172 int linesize_align = 4;
175 int need_copy;
176
177 if (avctx->width <= 0) {
180 }
181 if (avctx->height <= 0) {
184 }
185
187 stride = avctx->width / 8 + (avctx->width & 7 ? 1 : 0);
190 else
191 stride = avpkt->size / avctx->height;
192
194
195 if (
stride == 0 || avpkt->size <
stride * avctx->height) {
198 }
199
201
202 if ((avctx->bits_per_coded_sample == 8 || avctx->bits_per_coded_sample == 4 ||
203 avctx->bits_per_coded_sample == 2 || avctx->bits_per_coded_sample == 1 ||
204 (avctx->bits_per_coded_sample == 0 && (
context->is_nut_pal8 ||
context->is_mono)) ) &&
206 (!avctx->codec_tag || avctx->codec_tag ==
MKTAG(
'r',
'a',
'w',
' ') ||
210 int row_bytes = avctx->width / 8 + (avctx->width & 7 ? 1 : 0);
213 avctx->height, 1);
214 } else
217 avctx->height, 1);
218 } else {
221 avctx->height, 1);
222 }
225
227
229 if (res < 0)
230 return res;
231
236 }
237
239 return res;
240
241 if (need_copy)
243 else
247
248 // 1, 2, 4 and 8 bpp in avi/mov, 1 and 8 bpp in nut
250 int i, j, row_pix = 0;
253 if (avctx->bits_per_coded_sample == 8 ||
context->is_nut_pal8 ||
context->is_mono) {
254 int pix_per_byte =
context->is_mono ? 8 : 1;
255 for (
i = 0, j = 0; j < buf_size &&
i<avpkt->size;
i++, j++) {
257 row_pix += pix_per_byte;
258 if (row_pix >= avctx->width) {
260 j += 16 - (j % 16) - 1;
261 row_pix = 0;
262 }
263 }
264 } else if (avctx->bits_per_coded_sample == 4) {
265 for (
i = 0, j = 0; 2 * j + 1 < buf_size &&
i<avpkt->size;
i++, j++) {
266 dst[2 * j + 0] = buf[
i] >> 4;
267 dst[2 * j + 1] = buf[
i] & 15;
268 row_pix += 2;
269 if (row_pix >= avctx->width) {
271 j += 8 - (j % 8) - 1;
272 row_pix = 0;
273 }
274 }
275 } else if (avctx->bits_per_coded_sample == 2) {
276 for (
i = 0, j = 0; 4 * j + 3 < buf_size &&
i<avpkt->size;
i++, j++) {
277 dst[4 * j + 0] = buf[
i] >> 6;
278 dst[4 * j + 1] = buf[
i] >> 4 & 3;
279 dst[4 * j + 2] = buf[
i] >> 2 & 3;
280 dst[4 * j + 3] = buf[
i] & 3;
281 row_pix += 4;
282 if (row_pix >= avctx->width) {
284 j += 4 - (j % 4) - 1;
285 row_pix = 0;
286 }
287 }
288 } else {
289 av_assert0(avctx->bits_per_coded_sample == 1);
290 for (
i = 0, j = 0; 8 * j + 7 < buf_size &&
i<avpkt->size;
i++, j++) {
291 dst[8 * j + 0] = buf[
i] >> 7;
292 dst[8 * j + 1] = buf[
i] >> 6 & 1;
293 dst[8 * j + 2] = buf[
i] >> 5 & 1;
294 dst[8 * j + 3] = buf[
i] >> 4 & 1;
295 dst[8 * j + 4] = buf[
i] >> 3 & 1;
296 dst[8 * j + 5] = buf[
i] >> 2 & 1;
297 dst[8 * j + 6] = buf[
i] >> 1 & 1;
298 dst[8 * j + 7] = buf[
i] & 1;
299 row_pix += 8;
300 if (row_pix >= avctx->width) {
302 j += 2 - (j % 2) - 1;
303 row_pix = 0;
304 }
305 }
306 }
307 linesize_align = 16;
309 }
else if (
context->is_lt_16bpp) {
311 int packed = (avctx->codec_tag & 0xFFFFFF) ==
MKTAG(
'B',
'I',
'T', 0);
312 int swap = avctx->codec_tag >> 24;
313
314 if (packed && swap) {
318 if (swap == 16)
319 context->bbdsp.bswap16_buf(
context->bitstream_buf, (
const uint16_t*)buf, buf_size / 2);
320 else if (swap == 32)
321 context->bbdsp.bswap_buf(
context->bitstream_buf, (
const uint32_t*)buf, buf_size / 4);
322 else
325 }
326
328 scale16be(avctx,
dst, buf, buf_size, packed);
329 else
330 scale16le(avctx,
dst, buf, buf_size, packed);
331
333 } else if (need_copy) {
334 memcpy(
frame->buf[0]->data, buf, buf_size);
335 buf =
frame->buf[0]->data;
336 }
337
338 if (avctx->codec_tag ==
MKTAG(
'A',
'V',
'1',
'x') ||
339 avctx->codec_tag ==
MKTAG(
'A',
'V',
'u',
'p'))
340 buf += buf_size -
context->frame_size;
341
343 if (buf_size <
len && ((avctx->codec_tag & 0xFFFFFF) !=
MKTAG(
'B',
'I',
'T', 0) || !need_copy)) {
344 av_log(avctx,
AV_LOG_ERROR,
"Invalid buffer size, packet size %d < expected frame_size %d\n", buf_size,
len);
347 }
348
350 buf, avctx->pix_fmt,
351 avctx->width, avctx->height, 1)) < 0) {
353 return res;
354 }
355
358
364 }
369 }
370
372 #if FF_API_PALETTE_HAS_CHANGED
374 frame->palette_has_changed = 1;
376 #endif
377 }
else if (
context->is_nut_pal8) {
378 int vid_size = avctx->width * avctx->height;
379 int pal_size = avpkt->size - vid_size;
380
382 const uint8_t *pal = avpkt->data + vid_size;
383 memcpy(
context->palette->data, pal, pal_size);
384 #if FF_API_PALETTE_HAS_CHANGED
386 frame->palette_has_changed = 1;
388 #endif
389 }
390 }
391 }
392
402 FFALIGN(
frame->linesize[0], linesize_align) * avctx->height <= buf_size)
404
406 FFALIGN(
frame->linesize[0], linesize_align) * avctx->height +
407 FFALIGN(
frame->linesize[1], linesize_align) * ((avctx->height + 1) / 2) <= buf_size) {
409 frame->data[1] += (la0 -
frame->linesize[0]) * avctx->height;
410 frame->linesize[0] = la0;
412 }
413
416 if (!
frame->buf[1]) {
419 }
421 }
422
424 ((
frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
425 frame->linesize[0] = (
frame->linesize[0] + 3) & ~3;
426
429
430 if (avctx->codec_tag ==
MKTAG(
'Y',
'V',
'1',
'2') ||
431 avctx->codec_tag ==
MKTAG(
'Y',
'V',
'1',
'6') ||
432 avctx->codec_tag ==
MKTAG(
'Y',
'V',
'2',
'4') ||
433 avctx->codec_tag ==
MKTAG(
'Y',
'V',
'U',
'9'))
435
436 if (avctx->codec_tag ==
AV_RL32(
"I420") && (avctx->width+1)*(avctx->height+1) * 3/2 == buf_size) {
437 frame->data[1] =
frame->data[1] + (avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height;
438 frame->data[2] =
frame->data[2] + ((avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height)*5/4;
439 }
440
441 if (avctx->codec_tag ==
AV_RL32(
"yuv2") &&
443 int x, y;
445 for (y = 0; y < avctx->height; y++) {
446 for (x = 0; x < avctx->width; x++)
447 line[2 * x + 1] ^= 0x80;
449 }
450 }
451
452 if (avctx->codec_tag ==
AV_RL32(
"b64a") &&
455 uint64_t v;
456 int x, y;
457 for (y = 0; y < avctx->height; y++) {
458 for (x = 0; x >> 3 < avctx->width; x += 8) {
461 }
463 }
464 }
465
466 if (avctx->field_order >
AV_FIELD_PROGRESSIVE) {
/* we have interlaced material flagged in container */
470 }
471
472 *got_frame = 1;
473 return buf_size;
474 }
475
477 {
479
482 return 0;
483 }
484
486 .
p.
name =
"rawvideo",
496 };