170 const uint8_t *buf = avpkt->data;
171 int buf_size = avpkt->size;
172 int linesize_align = 4;
177 if (avctx->width <= 0) {
178 av_log(avctx, AV_LOG_ERROR,
"width is not set\n");
179 return AVERROR_INVALIDDATA;
181 if (avctx->height <= 0) {
182 av_log(avctx, AV_LOG_ERROR,
"height is not set\n");
183 return AVERROR_INVALIDDATA;
187 stride = avctx->width / 8 + (avctx->width & 7 ? 1 : 0);
191 stride = avpkt->size / avctx->height;
196 av_log(avctx, AV_LOG_ERROR,
"Packet too small (%d)\n", avpkt->size);
197 return AVERROR_INVALIDDATA;
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',
' ') ||
208 context->is_1_2_4_8_bpp = 1;
209 if (context->is_mono) {
210 int row_bytes = avctx->width / 8 + (avctx->width & 7 ? 1 : 0);
211 context->frame_size = av_image_get_buffer_size(avctx->pix_fmt,
212 FFALIGN(row_bytes, 16) * 8,
219 context->is_lt_16bpp =
av_get_bits_per_pixel(
desc) == 16 && avctx->bits_per_coded_sample > 8 && avctx->bits_per_coded_sample < 16;
223 if (context->frame_size < 0)
224 return context->frame_size;
226 need_copy = !avpkt->buf || context->is_1_2_4_8_bpp || context->is_yuv2 || context->is_lt_16bpp;
232 if (context->tff >= 0) {
233 frame->flags |= AV_FRAME_FLAG_INTERLACED;
234 if (context->tff == 1)
235 frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
249 if (context->is_1_2_4_8_bpp) {
250 int i, j, row_pix = 0;
251 uint8_t *dst = frame->buf[0]->data;
252 buf_size = context->frame_size - (context->is_pal8 ? AVPALETTE_SIZE : 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) {
259 i += stride - (i % stride) - 1;
260 j += 16 - (j % 16) - 1;
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;
269 if (row_pix >= avctx->width) {
270 i += stride - (i % stride) - 1;
271 j += 8 - (j % 8) - 1;
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;
282 if (row_pix >= avctx->width) {
283 i += stride - (i % stride) - 1;
284 j += 4 - (j % 4) - 1;
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;
300 if (row_pix >= avctx->width) {
302 j += 2 - (j % 2) - 1;
309 }
else if (context->is_lt_16bpp) {
310 uint8_t *dst = frame->buf[0]->data;
311 int packed = (avctx->codec_tag & 0xFFFFFF) == MKTAG(
'B',
'I',
'T', 0);
312 int swap = avctx->codec_tag >> 24;
314 if (packed && swap) {
315 av_fast_padded_malloc(&context->bitstream_buf, &context->bitstream_buf_size, buf_size);
316 if (!context->bitstream_buf)
317 return AVERROR(ENOMEM);
319 context->bbdsp.bswap16_buf(context->bitstream_buf, (const uint16_t*)buf, buf_size / 2);
321 context->bbdsp.bswap_buf(context->bitstream_buf, (const uint32_t*)buf, buf_size / 4);
323 return AVERROR_INVALIDDATA;
324 buf = context->bitstream_buf;
328 scale16be(avctx,
dst, buf, buf_size, packed);
330 scale16le(avctx,
dst, buf, buf_size, packed);
333 }
else if (need_copy) {
334 memcpy(
frame->buf[0]->data, buf, buf_size);
335 buf =
frame->buf[0]->data;
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;
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);
351 avctx->width, avctx->height, 1)) < 0) {
359 if (!context->palette)
361 if (!context->palette) {
371 if (!
ff_copy_palette(context->palette->data, avpkt, avctx) && context->is_nut_pal8) {
372 int vid_size = avctx->width * avctx->height;
373 int pal_size = avpkt->size - vid_size;
376 const uint8_t *pal = avpkt->data + vid_size;
377 memcpy(context->palette->data, pal, pal_size);
391 FFALIGN(
frame->linesize[0], linesize_align) * avctx->height <= buf_size)
395 FFALIGN(
frame->linesize[0], linesize_align) * avctx->height +
396 FFALIGN(
frame->linesize[1], linesize_align) * ((avctx->height + 1) / 2) <= buf_size) {
398 frame->data[1] += (la0 -
frame->linesize[0]) * avctx->height;
399 frame->linesize[0] = la0;
405 if (!
frame->buf[1]) {
413 ((
frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
414 frame->linesize[0] = (
frame->linesize[0] + 3) & ~3;
419 if (avctx->codec_tag ==
MKTAG(
'Y',
'V',
'1',
'2') ||
420 avctx->codec_tag ==
MKTAG(
'Y',
'V',
'1',
'6') ||
421 avctx->codec_tag ==
MKTAG(
'Y',
'V',
'2',
'4') ||
422 avctx->codec_tag ==
MKTAG(
'Y',
'V',
'U',
'9'))
425 if (avctx->codec_tag ==
AV_RL32(
"I420") && (avctx->width+1)*(avctx->height+1) * 3/2 == buf_size) {
426 frame->data[1] =
frame->data[1] + (avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height;
427 frame->data[2] =
frame->data[2] + ((avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height)*5/4;
430 if (avctx->codec_tag ==
AV_RL32(
"yuv2") &&
434 for (y = 0; y < avctx->height; y++) {
435 for (x = 0; x < avctx->width; x++)
436 line[2 * x + 1] ^= 0x80;
441 if (avctx->codec_tag ==
AV_RL32(
"b64a") &&
446 for (y = 0; y < avctx->height; y++) {
447 for (x = 0; x >> 3 < avctx->width; x += 8) {
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').