56 #define BS_8BIT_PEL (1 << 1)
57 #define BS_KEYFRAME (1 << 2)
58 #define BS_MV_Y_HALF (1 << 4)
59 #define BS_MV_X_HALF (1 << 5)
60 #define BS_NONREF (1 << 8)
64 typedef struct Plane {
72 #define CELL_STACK_MAX 20
121 static const int8_t offsets[8] = { 1, 1, 2, -3, -3, 3, 4, 4 };
122 static const 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];
157 for (p = 0; p < 3; p++) {
168 int p, chroma_width, chroma_height;
169 int luma_pitch, chroma_pitch, luma_size, chroma_size;
171 if (luma_width < 16 || luma_width > 640 ||
172 luma_height < 16 || luma_height > 480 ||
173 luma_width & 3 || luma_height & 3) {
175 luma_width, luma_height);
179 ctx->
width = luma_width ;
180 ctx->
height = luma_height;
182 chroma_width =
FFALIGN(luma_width >> 2, 4);
183 chroma_height =
FFALIGN(luma_height >> 2, 4);
185 luma_pitch =
FFALIGN(luma_width, 16);
186 chroma_pitch =
FFALIGN(chroma_width, 16);
190 luma_size = luma_pitch * (luma_height + 1);
194 chroma_size = chroma_pitch * (chroma_height + 1);
197 for (p = 0; p < 3; p++) {
198 ctx->
planes[p].
pitch = !p ? luma_pitch : chroma_pitch;
199 ctx->
planes[p].
width = !p ? luma_width : chroma_width;
200 ctx->
planes[p].
height = !p ? luma_height : chroma_height;
234 int h, w, mv_x, mv_y,
offset, offset_dst;
238 offset_dst = (cell->
ypos << 2) * plane->
pitch + (cell->
xpos << 2);
247 if ((cell->
ypos << 2) + mv_y < -1 || (cell->
xpos << 2) + mv_x < 0 ||
251 "Motion vectors point out of the frame.\n");
255 offset = offset_dst + mv_y * plane->
pitch + mv_x;
260 for (w = cell->
width; w > 0;) {
262 if (!((cell->
xpos << 2) & 15) && w >= 4) {
263 for (; w >= 4; src += 16, dst += 16, w -= 4)
268 if (!((cell->
xpos << 2) & 7) && w >= 2) {
286 #define AVG_32(dst, src, ref) \
287 AV_WN32A(dst, ((AV_RN32(src) + AV_RN32(ref)) >> 1) & 0x7F7F7F7FUL)
289 #define AVG_64(dst, src, ref) \
290 AV_WN64A(dst, ((AV_RN64(src) + AV_RN64(ref)) >> 1) & 0x7F7F7F7F7F7F7F7FULL)
299 a &= 0xFF00FF00FF00FF00ULL;
302 a &= 0x00FF00FF00FF00FFULL;
324 for (; n > 0; dst += row_offset, n--)
340 #define BUFFER_PRECHECK \
341 if (*data_ptr >= last_ptr) \
342 return IV3_OUT_OF_DATA; \
344 #define RLE_BLOCK_COPY \
345 if (cell->mv_ptr || !skip_flag) \
346 copy_block4(dst, ref, row_offset, row_offset, 4 << v_zoom)
348 #define RLE_BLOCK_COPY_8 \
349 pix64 = AV_RN64(ref);\
351 pix64 = replicate64(pix64);\
352 fill_64(dst + row_offset, pix64, 7, row_offset);\
353 AVG_64(dst, ref, dst + row_offset);\
355 fill_64(dst, pix64, 8, row_offset)
357 #define RLE_LINES_COPY \
358 copy_block4(dst, ref, row_offset, row_offset, num_lines << v_zoom)
360 #define RLE_LINES_COPY_M10 \
361 pix64 = AV_RN64(ref);\
362 if (is_top_of_cell) {\
363 pix64 = replicate64(pix64);\
364 fill_64(dst + row_offset, pix64, (num_lines << 1) - 1, row_offset);\
365 AVG_64(dst, ref, dst + row_offset);\
367 fill_64(dst, pix64, num_lines << 1, row_offset)
369 #define APPLY_DELTA_4 \
370 AV_WN16A(dst + line_offset ,\
371 (AV_RN16(ref ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
372 AV_WN16A(dst + line_offset + 2,\
373 (AV_RN16(ref + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
375 if (is_top_of_cell && !cell->ypos) {\
376 AV_COPY32U(dst, dst + row_offset);\
378 AVG_32(dst, ref, dst + row_offset);\
382 #define APPLY_DELTA_8 \
384 if (is_top_of_cell) { \
385 AV_WN32A(dst + row_offset , \
386 (replicate32(AV_RN32(ref )) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
387 AV_WN32A(dst + row_offset + 4, \
388 (replicate32(AV_RN32(ref + 4)) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
390 AV_WN32A(dst + row_offset , \
391 (AV_RN32(ref ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
392 AV_WN32A(dst + row_offset + 4, \
393 (AV_RN32(ref + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
398 if (is_top_of_cell && !cell->ypos) {\
399 AV_COPY64U(dst, dst + row_offset);\
401 AVG_64(dst, ref, dst + row_offset);
404 #define APPLY_DELTA_1011_INTER \
407 (AV_RN32(dst ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
409 (AV_RN32(dst + 4 ) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
410 AV_WN32A(dst + row_offset , \
411 (AV_RN32(dst + row_offset ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
412 AV_WN32A(dst + row_offset + 4, \
413 (AV_RN32(dst + row_offset + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
416 (AV_RN16(dst ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
418 (AV_RN16(dst + 2 ) + delta_tab->deltas[dyad2]) & 0x7F7F);\
419 AV_WN16A(dst + row_offset , \
420 (AV_RN16(dst + row_offset ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
421 AV_WN16A(dst + row_offset + 2, \
422 (AV_RN16(dst + row_offset + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
428 int pitch,
int h_zoom,
int v_zoom,
int mode,
432 int x,
y,
line, num_lines;
436 unsigned int dyad1, dyad2;
438 int skip_flag = 0, is_top_of_cell, is_first_row = 1;
439 int row_offset, blk_row_offset, line_offset;
442 blk_row_offset = (row_offset << (2 + v_zoom)) - (cell->
width << 2);
443 line_offset = v_zoom ? row_offset : 0;
448 for (y = 0; y < cell->
height; is_first_row = 0, y += 1 + v_zoom) {
449 for (x = 0; x < cell->
width; x += 1 + h_zoom) {
453 if (rle_blocks > 0) {
456 }
else if (mode == 10 && !cell->
mv_ptr) {
461 for (line = 0; line < 4;) {
463 is_top_of_cell = is_first_row && !line;
467 delta_tab = delta[line & 1];
469 delta_tab = delta[1];
471 code = bytestream_get_byte(data_ptr);
473 if (code < delta_tab->num_dyads) {
475 dyad1 = bytestream_get_byte(data_ptr);
477 if (dyad1 >= delta_tab->
num_dyads || dyad1 >= 248)
484 if (swap_quads[line & 1])
485 FFSWAP(
unsigned int, dyad1, dyad2);
489 }
else if (mode == 10 && !cell->
mv_ptr) {
505 num_lines = 257 - code - line;
510 }
else if (mode == 10 && !cell->
mv_ptr) {
516 code = bytestream_get_byte(data_ptr);
517 rle_blocks = (code & 0x1F) - 1;
518 if (code >= 64 || rle_blocks < 0)
520 skip_flag = code & 0x20;
521 num_lines = 4 - line;
522 if (mode >= 10 || (cell->
mv_ptr || !skip_flag)) {
525 }
else if (mode == 10 && !cell->
mv_ptr) {
541 }
else if (mode == 10 && !cell->
mv_ptr) {
552 ref += row_offset * (num_lines << v_zoom);
553 dst += row_offset * (num_lines << v_zoom);
558 block += 4 << h_zoom;
559 ref_block += 4 << h_zoom;
563 ref_block += blk_row_offset;
564 block += blk_row_offset;
587 int x, mv_x, mv_y,
mode, vq_index, prim_indx, second_indx;
589 int offset, error = 0, swap_quads[2];
592 const uint8_t *data_start = data_ptr;
597 vq_index = code & 0xF;
600 offset = (cell->
ypos << 2) * plane->
pitch + (cell->
xpos << 2);
605 ref_block = block - plane->
pitch;
606 }
else if (mode >= 10) {
618 if ((cell->
ypos << 2) + mv_y < -1 || (cell->
xpos << 2) + mv_x < 0 ||
622 "Motion vectors point out of the frame.\n");
626 offset += mv_y * plane->
pitch + mv_x;
633 if (mode == 1 || mode == 4) {
635 prim_indx = (code >> 4) + ctx->
cb_offset;
636 second_indx = (code & 0xF) + ctx->
cb_offset;
639 prim_indx = second_indx = vq_index;
642 if (prim_indx >= 24 || second_indx >= 24) {
643 av_log(avctx,
AV_LOG_ERROR,
"Invalid VQ table indexes! Primary: %d, secondary: %d!\n",
644 prim_indx, second_indx);
648 delta[0] = &
vq_tab[second_indx];
649 delta[1] = &
vq_tab[prim_indx];
650 swap_quads[0] = second_indx >= 16;
651 swap_quads[1] = prim_indx >= 16;
655 if (vq_index >= 8 && ref_block) {
656 for (x = 0; x < cell->
width << 2; x++)
657 ref_block[x] =
requant_tab[vq_index & 7][ref_block[x] & 127];
667 if (mode >= 3 && cell->
mv_ptr) {
672 zoom_fac = mode >= 3;
674 0, zoom_fac, mode, delta, swap_quads,
675 &data_ptr, last_ptr);
679 if (mode == 10 && !cell->
mv_ptr) {
681 1, 1, mode, delta, swap_quads,
682 &data_ptr, last_ptr);
684 if (mode == 11 && !cell->
mv_ptr) {
689 zoom_fac = mode == 10;
691 zoom_fac, 1, mode, delta, swap_quads,
692 &data_ptr, last_ptr);
702 av_log(avctx,
AV_LOG_ERROR,
"Mode %d: RLE code %X is not allowed at the current line\n",
712 av_log(avctx,
AV_LOG_ERROR,
"Mode %d: unsupported RLE code: %X\n", mode, data_ptr[-1]);
719 return data_ptr - data_start;
732 #define SPLIT_CELL(size, new_size) (new_size) = ((size) > 2) ? ((((size) + 2) >> 2) << 1) : 1
734 #define UPDATE_BITPOS(n) \
735 ctx->skip_bits += (n); \
738 #define RESYNC_BITSTREAM \
739 if (ctx->need_resync && !(get_bits_count(&ctx->gb) & 7)) { \
740 skip_bits_long(&ctx->gb, ctx->skip_bits); \
741 ctx->skip_bits = 0; \
742 ctx->need_resync = 0; \
746 if (curr_cell.xpos + curr_cell.width > (plane->width >> 2) || \
747 curr_cell.ypos + curr_cell.height > (plane->height >> 2)) { \
748 av_log(avctx, AV_LOG_ERROR, "Invalid cell: x=%d, y=%d, w=%d, h=%d\n", \
749 curr_cell.xpos, curr_cell.ypos, curr_cell.width, curr_cell.height); \
750 return AVERROR_INVALIDDATA; \
756 const int depth,
const int strip_width)
766 curr_cell = *ref_cell;
774 if (curr_cell.
width > strip_width) {
776 curr_cell.
width = (curr_cell.
width <= (strip_width << 1) ? 1 : 2) * strip_width;
781 if (ref_cell->
width <= 0 || curr_cell.
width <= 0)
790 if (
parse_bintree(ctx, avctx, plane, code, &curr_cell, depth - 1, strip_width))
794 if (!curr_cell.
tree) {
816 if (!curr_cell.
tree) {
838 bytes_used =
decode_cell(ctx, avctx, plane, &curr_cell,
860 unsigned num_vectors;
864 num_vectors = bytestream_get_le32(&data); data_size -= 4;
865 if (num_vectors > 256) {
867 "Read invalid number of motion vectors %d\n", num_vectors);
870 if (num_vectors * 2 > data_size)
877 init_get_bits(&ctx->
gb, &data[num_vectors * 2], (data_size - num_vectors * 2) << 3);
884 curr_cell.
xpos = curr_cell.
ypos = 0;
894 #define OS_HDR_ID MKBETAG('F', 'R', 'M', 'H')
901 uint32_t frame_num, word2, check_sum, data_size;
902 uint32_t y_offset, u_offset, v_offset, starts[3], ends[3];
909 frame_num = bytestream2_get_le32(&gb);
910 word2 = bytestream2_get_le32(&gb);
911 check_sum = bytestream2_get_le32(&gb);
912 data_size = bytestream2_get_le32(&gb);
914 if ((frame_num ^ word2 ^ data_size ^
OS_HDR_ID) != check_sum) {
922 if (bytestream2_get_le16(&gb) != 32) {
929 ctx->
data_size = (bytestream2_get_le32(&gb) + 7) >> 3;
930 ctx->
cb_offset = bytestream2_get_byte(&gb);
939 height = bytestream2_get_le16(&gb);
940 width = bytestream2_get_le16(&gb);
944 if (width != ctx->
width || height != ctx->
height) {
947 av_dlog(avctx,
"Frame dimensions changed!\n");
949 if (width < 16 || width > 640 ||
950 height < 16 || height > 480 ||
951 width & 3 || height & 3) {
953 "Invalid picture dimensions: %d x %d!\n", width, height);
963 y_offset = bytestream2_get_le32(&gb);
964 v_offset = bytestream2_get_le32(&gb);
965 u_offset = bytestream2_get_le32(&gb);
970 starts[0] = y_offset;
971 starts[1] = v_offset;
972 starts[2] = u_offset;
974 for (j = 0; j < 3; j++) {
976 for (i = 2; i >= 0; i--)
977 if (starts[i] < ends[j] && starts[i] > starts[j])
985 FFMIN3(y_offset, v_offset, u_offset) < gb.
buffer - bs_hdr + 16 ||
1030 uint32_t pitch = plane->
pitch;
1033 for (y = 0; y < dst_height; y++) {
1035 for (x = 0; x < plane->
width >> 2; x++) {
1041 for (x <<= 2; x < plane->
width; x++)
1042 *dst++ = *src++ << 1;
1044 src += pitch - plane->
width;
1045 dst += dst_pitch - plane->
width;
1070 int buf_size = avpkt->
size;
1116 (avctx->
height + 3) >> 2);
1119 (avctx->
height + 3) >> 2);