55 #define BS_8BIT_PEL (1 << 1)
56 #define BS_KEYFRAME (1 << 2)
57 #define BS_MV_Y_HALF (1 << 4)
58 #define BS_MV_X_HALF (1 << 5)
59 #define BS_NONREF (1 << 8)
71 #define CELL_STACK_MAX 20
121 static int8_t offsets[8] = { 1, 1, 2, -3, -3, 3, 4, 4 };
122 static int8_t deltas [8] = { 0, 1, 0, 4, 4, 1, 0, 1 };
126 for (i = 0; i < 8; i++) {
128 for (j = 0; j < 128; j++)
129 requant_tab[i][j] = (j + offsets[i]) / step * step + deltas[i];
154 int p, luma_width, luma_height, chroma_width, chroma_height;
155 int luma_pitch, chroma_pitch, luma_size, chroma_size;
157 luma_width = ctx->
width;
158 luma_height = ctx->
height;
160 if (luma_width < 16 || luma_width > 640 ||
161 luma_height < 16 || luma_height > 480 ||
162 luma_width & 3 || luma_height & 3) {
164 luma_width, luma_height);
168 chroma_width =
FFALIGN(luma_width >> 2, 4);
169 chroma_height =
FFALIGN(luma_height >> 2, 4);
171 luma_pitch =
FFALIGN(luma_width, 16);
172 chroma_pitch =
FFALIGN(chroma_width, 16);
176 luma_size = luma_pitch * (luma_height + 1);
180 chroma_size = chroma_pitch * (chroma_height + 1);
183 for (p = 0; p < 3; p++) {
184 ctx->
planes[p].
pitch = !p ? luma_pitch : chroma_pitch;
185 ctx->
planes[p].
width = !p ? luma_width : chroma_width;
186 ctx->
planes[p].
height = !p ? luma_height : chroma_height;
210 for (p = 0; p < 3; p++) {
228 int h, w, mv_x, mv_y, offset, offset_dst;
232 offset_dst = (cell->
ypos << 2) * plane->
pitch + (cell->
xpos << 2);
238 if ((cell->
ypos << 2) + mv_y < -1 || (cell->
xpos << 2) + mv_x < 0 ||
242 "Motion vectors point out of the frame.\n");
246 offset = offset_dst + mv_y * plane->
pitch + mv_x;
251 for (w = cell->
width; w > 0;) {
253 if (!((cell->
xpos << 2) & 15) && w >= 4) {
254 for (; w >= 4; src += 16, dst += 16, w -= 4)
259 if (!((cell->
xpos << 2) & 7) && w >= 2) {
279 #define AVG_32(dst, src, ref) \
280 AV_WN32A(dst, ((AV_RN32A(src) + AV_RN32A(ref)) >> 1) & 0x7F7F7F7FUL)
282 #define AVG_64(dst, src, ref) \
283 AV_WN64A(dst, ((AV_RN64A(src) + AV_RN64A(ref)) >> 1) & 0x7F7F7F7F7F7F7F7FULL)
292 a &= 0xFF00FF00FF00FF00ULL;
295 a &= 0x00FF00FF00FF00FFULL;
314 static inline void fill_64(uint8_t *dst,
const uint64_t pix, int32_t n,
317 for (; n > 0; dst += row_offset, n--)
333 #define BUFFER_PRECHECK \
334 if (*data_ptr >= last_ptr) \
335 return IV3_OUT_OF_DATA; \
337 #define RLE_BLOCK_COPY \
338 if (cell->mv_ptr || !skip_flag) \
339 copy_block4(dst, ref, row_offset, row_offset, 4 << v_zoom)
341 #define RLE_BLOCK_COPY_8 \
342 pix64 = AV_RN64A(ref);\
344 pix64 = replicate64(pix64);\
345 fill_64(dst + row_offset, pix64, 7, row_offset);\
346 AVG_64(dst, ref, dst + row_offset);\
348 fill_64(dst, pix64, 8, row_offset)
350 #define RLE_LINES_COPY \
351 copy_block4(dst, ref, row_offset, row_offset, num_lines << v_zoom)
353 #define RLE_LINES_COPY_M10 \
354 pix64 = AV_RN64A(ref);\
355 if (is_top_of_cell) {\
356 pix64 = replicate64(pix64);\
357 fill_64(dst + row_offset, pix64, (num_lines << 1) - 1, row_offset);\
358 AVG_64(dst, ref, dst + row_offset);\
360 fill_64(dst, pix64, num_lines << 1, row_offset)
362 #define APPLY_DELTA_4 \
363 AV_WN16A(dst + line_offset ,\
364 (AV_RN16A(ref ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
365 AV_WN16A(dst + line_offset + 2,\
366 (AV_RN16A(ref + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
368 if (is_top_of_cell && !cell->ypos) {\
369 AV_COPY32(dst, dst + row_offset);\
371 AVG_32(dst, ref, dst + row_offset);\
375 #define APPLY_DELTA_8 \
377 if (is_top_of_cell) { \
378 AV_WN32A(dst + row_offset , \
379 (replicate32(AV_RN32A(ref )) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
380 AV_WN32A(dst + row_offset + 4, \
381 (replicate32(AV_RN32A(ref + 4)) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
383 AV_WN32A(dst + row_offset , \
384 (AV_RN32A(ref ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
385 AV_WN32A(dst + row_offset + 4, \
386 (AV_RN32A(ref + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
391 if (is_top_of_cell && !cell->ypos) {\
392 AV_COPY64(dst, dst + row_offset);\
394 AVG_64(dst, ref, dst + row_offset);
397 #define APPLY_DELTA_1011_INTER \
400 (AV_RN32A(dst ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
402 (AV_RN32A(dst + 4 ) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
403 AV_WN32A(dst + row_offset , \
404 (AV_RN32A(dst + row_offset ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
405 AV_WN32A(dst + row_offset + 4, \
406 (AV_RN32A(dst + row_offset + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
409 (AV_RN16A(dst ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
411 (AV_RN16A(dst + 2 ) + delta_tab->deltas[dyad2]) & 0x7F7F);\
412 AV_WN16A(dst + row_offset , \
413 (AV_RN16A(dst + row_offset ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
414 AV_WN16A(dst + row_offset + 2, \
415 (AV_RN16A(dst + row_offset + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
420 int pitch,
int h_zoom,
int v_zoom,
int mode,
422 const uint8_t **data_ptr,
const uint8_t *last_ptr)
424 int x, y,
line, num_lines;
426 uint8_t code, *dst, *ref;
428 unsigned int dyad1, dyad2;
430 int skip_flag = 0, is_top_of_cell, is_first_row = 1;
431 int row_offset, blk_row_offset, line_offset;
434 blk_row_offset = (row_offset << (2 + v_zoom)) - (cell->
width << 2);
435 line_offset = v_zoom ? row_offset : 0;
440 for (y = 0; y < cell->
height; is_first_row = 0, y += 1 + v_zoom) {
441 for (x = 0; x < cell->
width; x += 1 + h_zoom) {
445 if (rle_blocks > 0) {
448 }
else if (mode == 10 && !cell->
mv_ptr) {
453 for (line = 0; line < 4;) {
455 is_top_of_cell = is_first_row && !line;
459 delta_tab = delta[line & 1];
461 delta_tab = delta[1];
463 code = bytestream_get_byte(data_ptr);
465 if (code < delta_tab->num_dyads) {
467 dyad1 = bytestream_get_byte(data_ptr);
469 if (dyad1 >= delta_tab->
num_dyads || dyad1 >= 248)
476 if (swap_quads[line & 1])
477 FFSWAP(
unsigned int, dyad1, dyad2);
481 }
else if (mode == 10 && !cell->
mv_ptr) {
497 num_lines = 257 - code - line;
502 }
else if (mode == 10 && !cell->
mv_ptr) {
508 code = bytestream_get_byte(data_ptr);
509 rle_blocks = (code & 0x1F) - 1;
510 if (code >= 64 || rle_blocks < 0)
512 skip_flag = code & 0x20;
513 num_lines = 4 - line;
514 if (mode >= 10 || (cell->
mv_ptr || !skip_flag)) {
517 }
else if (mode == 10 && !cell->
mv_ptr) {
533 }
else if (mode == 10 && !cell->
mv_ptr) {
544 ref += row_offset * (num_lines << v_zoom);
545 dst += row_offset * (num_lines << v_zoom);
550 block += 4 << h_zoom;
551 ref_block += 4 << h_zoom;
555 ref_block += blk_row_offset;
556 block += blk_row_offset;
577 const uint8_t *last_ptr)
579 int x, mv_x, mv_y, mode, vq_index, prim_indx, second_indx;
581 int offset, error = 0, swap_quads[2];
582 uint8_t code, *
block, *ref_block = 0;
584 const uint8_t *data_start = data_ptr;
589 vq_index = code & 0xF;
592 offset = (cell->
ypos << 2) * plane->
pitch + (cell->
xpos << 2);
596 ref_block = block - plane->
pitch;
597 }
else if (mode >= 10) {
609 if ((cell->
ypos << 2) + mv_y < -1 || (cell->
xpos << 2) + mv_x < 0 ||
613 "Motion vectors point out of the frame.\n");
617 offset += mv_y * plane->
pitch + mv_x;
624 if (mode == 1 || mode == 4) {
626 prim_indx = (code >> 4) + ctx->
cb_offset;
627 second_indx = (code & 0xF) + ctx->
cb_offset;
630 prim_indx = second_indx = vq_index;
633 if (prim_indx >= 24 || second_indx >= 24) {
634 av_log(avctx,
AV_LOG_ERROR,
"Invalid VQ table indexes! Primary: %d, secondary: %d!\n",
635 prim_indx, second_indx);
639 delta[0] = &
vq_tab[second_indx];
640 delta[1] = &
vq_tab[prim_indx];
641 swap_quads[0] = second_indx >= 16;
642 swap_quads[1] = prim_indx >= 16;
646 if (vq_index >= 8 && ref_block) {
647 for (x = 0; x < cell->
width << 2; x++)
648 ref_block[x] =
requant_tab[vq_index & 7][ref_block[x]];
658 if (mode >= 3 && cell->
mv_ptr) {
663 zoom_fac = mode >= 3;
665 mode, delta, swap_quads, &data_ptr, last_ptr);
669 if (mode == 10 && !cell->
mv_ptr) {
671 mode, delta, swap_quads, &data_ptr, last_ptr);
673 if (mode == 11 && !cell->
mv_ptr) {
678 zoom_fac = mode == 10;
680 zoom_fac, 1, mode, delta, swap_quads,
681 &data_ptr, last_ptr);
691 av_log(avctx,
AV_LOG_ERROR,
"Mode %d: RLE code %X is not allowed at the current line\n",
701 av_log(avctx,
AV_LOG_ERROR,
"Mode %d: unsupported RLE code: %X\n", mode, data_ptr[-1]);
708 return data_ptr - data_start;
721 #define SPLIT_CELL(size, new_size) (new_size) = ((size) > 2) ? ((((size) + 2) >> 2) << 1) : 1
723 #define UPDATE_BITPOS(n) \
724 ctx->skip_bits += (n); \
727 #define RESYNC_BITSTREAM \
728 if (ctx->need_resync && !(get_bits_count(&ctx->gb) & 7)) { \
729 skip_bits_long(&ctx->gb, ctx->skip_bits); \
730 ctx->skip_bits = 0; \
731 ctx->need_resync = 0; \
735 if (curr_cell.xpos + curr_cell.width > (plane->width >> 2) || \
736 curr_cell.ypos + curr_cell.height > (plane->height >> 2)) { \
737 av_log(avctx, AV_LOG_ERROR, "Invalid cell: x=%d, y=%d, w=%d, h=%d\n", \
738 curr_cell.xpos, curr_cell.ypos, curr_cell.width, curr_cell.height); \
739 return AVERROR_INVALIDDATA; \
745 const int depth,
const int strip_width)
755 curr_cell = *ref_cell;
763 if (curr_cell.
width > strip_width) {
765 curr_cell.
width = (curr_cell.
width <= (strip_width << 1) ? 1 : 2) * strip_width;
770 if (ref_cell->
width <= 0 || curr_cell.
width <= 0)
779 if (
parse_bintree(ctx, avctx, plane, code, &curr_cell, depth - 1, strip_width))
783 if (!curr_cell.
tree) {
804 if (!curr_cell.
tree) {
822 bytes_used =
decode_cell(ctx, avctx, plane, &curr_cell,
840 Plane *plane,
const uint8_t *
data, int32_t data_size,
844 unsigned num_vectors;
848 num_vectors = bytestream_get_le32(&data);
849 if (num_vectors > 256) {
851 "Read invalid number of motion vectors %d\n", num_vectors);
854 if (num_vectors * 2 >= data_size)
861 init_get_bits(&ctx->
gb, &data[num_vectors * 2], (data_size - num_vectors * 2) << 3);
868 curr_cell.
xpos = curr_cell.
ypos = 0;
878 #define OS_HDR_ID MKBETAG('F', 'R', 'M', 'H')
881 const uint8_t *buf,
int buf_size)
884 const uint8_t *bs_hdr;
885 uint32_t frame_num, word2, check_sum, data_size;
886 uint32_t y_offset, u_offset, v_offset, starts[3], ends[3];
893 frame_num = bytestream2_get_le32(&gb);
894 word2 = bytestream2_get_le32(&gb);
895 check_sum = bytestream2_get_le32(&gb);
896 data_size = bytestream2_get_le32(&gb);
898 if ((frame_num ^ word2 ^ data_size ^
OS_HDR_ID) != check_sum) {
906 if (bytestream2_get_le16(&gb) != 32) {
913 ctx->
data_size = (bytestream2_get_le32(&gb) + 7) >> 3;
914 ctx->
cb_offset = bytestream2_get_byte(&gb);
923 height = bytestream2_get_le16(&gb);
924 width = bytestream2_get_le16(&gb);
928 if (width != ctx->
width || height != ctx->
height) {
931 av_dlog(avctx,
"Frame dimensions changed!\n");
933 if (width < 16 || width > 640 ||
934 height < 16 || height > 480 ||
935 width & 3 || height & 3) {
937 "Invalid picture dimensions: %d x %d!\n", width, height);
950 y_offset = bytestream2_get_le32(&gb);
951 v_offset = bytestream2_get_le32(&gb);
952 u_offset = bytestream2_get_le32(&gb);
957 starts[0] = y_offset;
958 starts[1] = v_offset;
959 starts[2] = u_offset;
961 for (j = 0; j < 3; j++) {
963 for (i = 2; i >= 0; i--)
964 if (starts[i] < ends[j] && starts[i] > starts[j])
972 FFMIN3(y_offset, v_offset, u_offset) < gb.
buffer - bs_hdr + 16 ||
1014 const uint8_t *src = plane->
pixels[buf_sel];
1015 uint32_t pitch = plane->
pitch;
1017 for (y = 0; y < plane->
height; y++) {
1019 for (x = 0; x < plane->
width >> 2; x++) {
1025 for (x <<= 2; x < plane->
width; x++)
1026 *dst++ = *src++ << 1;
1028 src += pitch - plane->
width;
1029 dst += dst_pitch - plane->
width;
1057 const uint8_t *buf = avpkt->
data;
1058 int buf_size = avpkt->
size;