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)
63 typedef struct Plane {
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, chroma_width, chroma_height;
155 int luma_pitch, chroma_pitch, luma_size, chroma_size;
157 if (luma_width < 16 || luma_width > 640 ||
158 luma_height < 16 || luma_height > 480 ||
159 luma_width & 3 || luma_height & 3) {
161 luma_width, luma_height);
165 ctx->
width = luma_width ;
166 ctx->
height = 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;
213 for (p = 0; p < 3; p++) {
231 int h, w, mv_x, mv_y,
offset, offset_dst;
235 offset_dst = (cell->
ypos << 2) * plane->
pitch + (cell->
xpos << 2);
244 if ((cell->
ypos << 2) + mv_y < -1 || (cell->
xpos << 2) + mv_x < 0 ||
248 "Motion vectors point out of the frame.\n");
252 offset = offset_dst + mv_y * plane->
pitch + mv_x;
257 for (w = cell->
width; w > 0;) {
259 if (!((cell->
xpos << 2) & 15) && w >= 4) {
260 for (; w >= 4; src += 16, dst += 16, w -= 4)
265 if (!((cell->
xpos << 2) & 7) && w >= 2) {
285 #define AVG_32(dst, src, ref) \
286 AV_WN32A(dst, ((AV_RN32(src) + AV_RN32(ref)) >> 1) & 0x7F7F7F7FUL)
288 #define AVG_64(dst, src, ref) \
289 AV_WN64A(dst, ((AV_RN64(src) + AV_RN64(ref)) >> 1) & 0x7F7F7F7F7F7F7F7FULL)
298 a &= 0xFF00FF00FF00FF00ULL;
301 a &= 0x00FF00FF00FF00FFULL;
323 for (; n > 0; dst += row_offset, n--)
339 #define BUFFER_PRECHECK \
340 if (*data_ptr >= last_ptr) \
341 return IV3_OUT_OF_DATA; \
343 #define RLE_BLOCK_COPY \
344 if (cell->mv_ptr || !skip_flag) \
345 copy_block4(dst, ref, row_offset, row_offset, 4 << v_zoom)
347 #define RLE_BLOCK_COPY_8 \
348 pix64 = AV_RN64(ref);\
350 pix64 = replicate64(pix64);\
351 fill_64(dst + row_offset, pix64, 7, row_offset);\
352 AVG_64(dst, ref, dst + row_offset);\
354 fill_64(dst, pix64, 8, row_offset)
356 #define RLE_LINES_COPY \
357 copy_block4(dst, ref, row_offset, row_offset, num_lines << v_zoom)
359 #define RLE_LINES_COPY_M10 \
360 pix64 = AV_RN64(ref);\
361 if (is_top_of_cell) {\
362 pix64 = replicate64(pix64);\
363 fill_64(dst + row_offset, pix64, (num_lines << 1) - 1, row_offset);\
364 AVG_64(dst, ref, dst + row_offset);\
366 fill_64(dst, pix64, num_lines << 1, row_offset)
368 #define APPLY_DELTA_4 \
369 AV_WN16A(dst + line_offset ,\
370 (AV_RN16(ref ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
371 AV_WN16A(dst + line_offset + 2,\
372 (AV_RN16(ref + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
374 if (is_top_of_cell && !cell->ypos) {\
375 AV_COPY32U(dst, dst + row_offset);\
377 AVG_32(dst, ref, dst + row_offset);\
381 #define APPLY_DELTA_8 \
383 if (is_top_of_cell) { \
384 AV_WN32A(dst + row_offset , \
385 (replicate32(AV_RN32(ref )) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
386 AV_WN32A(dst + row_offset + 4, \
387 (replicate32(AV_RN32(ref + 4)) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
389 AV_WN32A(dst + row_offset , \
390 (AV_RN32(ref ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
391 AV_WN32A(dst + row_offset + 4, \
392 (AV_RN32(ref + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
397 if (is_top_of_cell && !cell->ypos) {\
398 AV_COPY64U(dst, dst + row_offset);\
400 AVG_64(dst, ref, dst + row_offset);
403 #define APPLY_DELTA_1011_INTER \
406 (AV_RN32(dst ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
408 (AV_RN32(dst + 4 ) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
409 AV_WN32A(dst + row_offset , \
410 (AV_RN32(dst + row_offset ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
411 AV_WN32A(dst + row_offset + 4, \
412 (AV_RN32(dst + row_offset + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
415 (AV_RN16(dst ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
417 (AV_RN16(dst + 2 ) + delta_tab->deltas[dyad2]) & 0x7F7F);\
418 AV_WN16A(dst + row_offset , \
419 (AV_RN16(dst + row_offset ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
420 AV_WN16A(dst + row_offset + 2, \
421 (AV_RN16(dst + row_offset + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
426 int pitch,
int h_zoom,
int v_zoom,
int mode,
430 int x, y,
line, num_lines;
434 unsigned int dyad1, dyad2;
436 int skip_flag = 0, is_top_of_cell, is_first_row = 1;
437 int row_offset, blk_row_offset, line_offset;
440 blk_row_offset = (row_offset << (2 + v_zoom)) - (cell->
width << 2);
441 line_offset = v_zoom ? row_offset : 0;
446 for (y = 0; y < cell->
height; is_first_row = 0, y += 1 + v_zoom) {
447 for (x = 0; x < cell->
width; x += 1 + h_zoom) {
451 if (rle_blocks > 0) {
454 }
else if (mode == 10 && !cell->
mv_ptr) {
459 for (line = 0; line < 4;) {
461 is_top_of_cell = is_first_row && !line;
465 delta_tab = delta[line & 1];
467 delta_tab = delta[1];
469 code = bytestream_get_byte(data_ptr);
471 if (code < delta_tab->num_dyads) {
473 dyad1 = bytestream_get_byte(data_ptr);
475 if (dyad1 >= delta_tab->
num_dyads || dyad1 >= 248)
482 if (swap_quads[line & 1])
483 FFSWAP(
unsigned int, dyad1, dyad2);
487 }
else if (mode == 10 && !cell->
mv_ptr) {
503 num_lines = 257 - code - line;
508 }
else if (mode == 10 && !cell->
mv_ptr) {
514 code = bytestream_get_byte(data_ptr);
515 rle_blocks = (code & 0x1F) - 1;
516 if (code >= 64 || rle_blocks < 0)
518 skip_flag = code & 0x20;
519 num_lines = 4 - line;
520 if (mode >= 10 || (cell->
mv_ptr || !skip_flag)) {
523 }
else if (mode == 10 && !cell->
mv_ptr) {
539 }
else if (mode == 10 && !cell->
mv_ptr) {
550 ref += row_offset * (num_lines << v_zoom);
551 dst += row_offset * (num_lines << v_zoom);
556 block += 4 << h_zoom;
557 ref_block += 4 << h_zoom;
561 ref_block += blk_row_offset;
562 block += blk_row_offset;
585 int x, mv_x, mv_y,
mode, vq_index, prim_indx, second_indx;
587 int offset, error = 0, swap_quads[2];
590 const uint8_t *data_start = data_ptr;
595 vq_index = code & 0xF;
598 offset = (cell->
ypos << 2) * plane->
pitch + (cell->
xpos << 2);
604 if ( mv_x + 4*cell->
xpos < 0
605 || mv_y + 4*cell->
ypos < 0
615 ref_block = block - plane->
pitch;
616 }
else if (mode >= 10) {
628 if ((cell->
ypos << 2) + mv_y < -1 || (cell->
xpos << 2) + mv_x < 0 ||
632 "Motion vectors point out of the frame.\n");
636 offset += mv_y * plane->
pitch + mv_x;
643 if (mode == 1 || mode == 4) {
645 prim_indx = (code >> 4) + ctx->
cb_offset;
646 second_indx = (code & 0xF) + ctx->
cb_offset;
649 prim_indx = second_indx = vq_index;
652 if (prim_indx >= 24 || second_indx >= 24) {
653 av_log(avctx,
AV_LOG_ERROR,
"Invalid VQ table indexes! Primary: %d, secondary: %d!\n",
654 prim_indx, second_indx);
658 delta[0] = &
vq_tab[second_indx];
659 delta[1] = &
vq_tab[prim_indx];
660 swap_quads[0] = second_indx >= 16;
661 swap_quads[1] = prim_indx >= 16;
665 if (vq_index >= 8 && ref_block) {
666 for (x = 0; x < cell->
width << 2; x++)
667 ref_block[x] =
requant_tab[vq_index & 7][ref_block[x] & 127];
677 if (mode >= 3 && cell->
mv_ptr) {
682 zoom_fac = mode >= 3;
684 mode, delta, swap_quads, &data_ptr, last_ptr);
688 if (mode == 10 && !cell->
mv_ptr) {
690 mode, delta, swap_quads, &data_ptr, last_ptr);
692 if (mode == 11 && !cell->
mv_ptr) {
697 zoom_fac = mode == 10;
699 zoom_fac, 1, mode, delta, swap_quads,
700 &data_ptr, last_ptr);
710 av_log(avctx,
AV_LOG_ERROR,
"Mode %d: RLE code %X is not allowed at the current line\n",
720 av_log(avctx,
AV_LOG_ERROR,
"Mode %d: unsupported RLE code: %X\n", mode, data_ptr[-1]);
727 return data_ptr - data_start;
740 #define SPLIT_CELL(size, new_size) (new_size) = ((size) > 2) ? ((((size) + 2) >> 2) << 1) : 1
742 #define UPDATE_BITPOS(n) \
743 ctx->skip_bits += (n); \
746 #define RESYNC_BITSTREAM \
747 if (ctx->need_resync && !(get_bits_count(&ctx->gb) & 7)) { \
748 skip_bits_long(&ctx->gb, ctx->skip_bits); \
749 ctx->skip_bits = 0; \
750 ctx->need_resync = 0; \
754 if (curr_cell.xpos + curr_cell.width > (plane->width >> 2) || \
755 curr_cell.ypos + curr_cell.height > (plane->height >> 2)) { \
756 av_log(avctx, AV_LOG_ERROR, "Invalid cell: x=%d, y=%d, w=%d, h=%d\n", \
757 curr_cell.xpos, curr_cell.ypos, curr_cell.width, curr_cell.height); \
758 return AVERROR_INVALIDDATA; \
764 const int depth,
const int strip_width)
774 curr_cell = *ref_cell;
782 if (curr_cell.
width > strip_width) {
784 curr_cell.
width = (curr_cell.
width <= (strip_width << 1) ? 1 : 2) * strip_width;
789 if (ref_cell->
width <= 0 || curr_cell.
width <= 0)
798 if (
parse_bintree(ctx, avctx, plane, code, &curr_cell, depth - 1, strip_width))
802 if (!curr_cell.
tree) {
823 if (!curr_cell.
tree) {
845 bytes_used =
decode_cell(ctx, avctx, plane, &curr_cell,
867 unsigned num_vectors;
871 num_vectors = bytestream_get_le32(&data); data_size -= 4;
872 if (num_vectors > 256) {
874 "Read invalid number of motion vectors %d\n", num_vectors);
877 if (num_vectors * 2 > data_size)
884 init_get_bits(&ctx->
gb, &data[num_vectors * 2], (data_size - num_vectors * 2) << 3);
891 curr_cell.
xpos = curr_cell.
ypos = 0;
901 #define OS_HDR_ID MKBETAG('F', 'R', 'M', 'H')
904 const uint8_t *buf,
int buf_size)
908 uint32_t frame_num, word2, check_sum, data_size;
909 uint32_t y_offset, u_offset, v_offset, starts[3], ends[3];
916 frame_num = bytestream2_get_le32(&gb);
917 word2 = bytestream2_get_le32(&gb);
918 check_sum = bytestream2_get_le32(&gb);
919 data_size = bytestream2_get_le32(&gb);
921 if ((frame_num ^ word2 ^ data_size ^
OS_HDR_ID) != check_sum) {
929 if (bytestream2_get_le16(&gb) != 32) {
936 ctx->
data_size = (bytestream2_get_le32(&gb) + 7) >> 3;
937 ctx->
cb_offset = bytestream2_get_byte(&gb);
946 height = bytestream2_get_le16(&gb);
947 width = bytestream2_get_le16(&gb);
951 if (width != ctx->
width || height != ctx->
height) {
954 av_dlog(avctx,
"Frame dimensions changed!\n");
956 if (width < 16 || width > 640 ||
957 height < 16 || height > 480 ||
958 width & 3 || height & 3) {
960 "Invalid picture dimensions: %d x %d!\n", width, height);
969 y_offset = bytestream2_get_le32(&gb);
970 v_offset = bytestream2_get_le32(&gb);
971 u_offset = bytestream2_get_le32(&gb);
976 starts[0] = y_offset;
977 starts[1] = v_offset;
978 starts[2] = u_offset;
980 for (j = 0; j < 3; j++) {
982 for (i = 2; i >= 0; i--)
983 if (starts[i] < ends[j] && starts[i] > starts[j])
991 FFMIN3(y_offset, v_offset, u_offset) < gb.
buffer - bs_hdr + 16 ||
1032 int dst_pitch,
int dst_height)
1036 uint32_t pitch = plane->
pitch;
1039 for (y = 0; y < dst_height; y++) {
1041 for (x = 0; x < plane->
width >> 2; x++) {
1047 for (x <<= 2; x < plane->
width; x++)
1048 *dst++ = *src++ << 1;
1050 src += pitch - plane->
width;
1051 dst += dst_pitch - plane->
width;
1077 int buf_size = avpkt->
size;
1128 (avctx->
height + 3) >> 2);
1131 (avctx->
height + 3) >> 2);