54 #define BS_8BIT_PEL (1 << 1)
55 #define BS_KEYFRAME (1 << 2)
56 #define BS_MV_Y_HALF (1 << 4)
57 #define BS_MV_X_HALF (1 << 5)
58 #define BS_NONREF (1 << 8)
70 #define CELL_STACK_MAX 20
120 static int8_t offsets[8] = { 1, 1, 2, -3, -3, 3, 4, 4 };
121 static int8_t deltas [8] = { 0, 1, 0, 4, 4, 1, 0, 1 };
125 for (i = 0; i < 8; i++) {
127 for (j = 0; j < 128; j++)
128 requant_tab[i][j] = (j + offsets[i]) / step * step + deltas[i];
153 int p, luma_width, luma_height, chroma_width, chroma_height;
154 int luma_pitch, chroma_pitch, luma_size, chroma_size;
156 luma_width = ctx->
width;
157 luma_height = ctx->
height;
159 if (luma_width < 16 || luma_width > 640 ||
160 luma_height < 16 || luma_height > 480 ||
161 luma_width & 3 || luma_height & 3) {
163 luma_width, luma_height);
167 chroma_width =
FFALIGN(luma_width >> 2, 4);
168 chroma_height =
FFALIGN(luma_height >> 2, 4);
170 luma_pitch =
FFALIGN(luma_width, 16);
171 chroma_pitch =
FFALIGN(chroma_width, 16);
175 luma_size = luma_pitch * (luma_height + 1);
179 chroma_size = chroma_pitch * (chroma_height + 1);
182 for (p = 0; p < 3; p++) {
183 ctx->
planes[p].
pitch = !p ? luma_pitch : chroma_pitch;
184 ctx->
planes[p].
width = !p ? luma_width : chroma_width;
185 ctx->
planes[p].
height = !p ? luma_height : chroma_height;
207 for (p = 0; p < 3; p++) {
225 int h, w, mv_x, mv_y, offset, offset_dst;
229 offset_dst = (cell->
ypos << 2) * plane->
pitch + (cell->
xpos << 2);
233 offset = offset_dst + mv_y * plane->
pitch + mv_x;
238 for (w = cell->
width; w > 0;) {
240 if (!((cell->
xpos << 2) & 15) && w >= 4) {
241 for (; w >= 4; src += 16, dst += 16, w -= 4)
246 if (!((cell->
xpos << 2) & 7) && w >= 2) {
264 #define AVG_32(dst, src, ref) \
265 AV_WN32A(dst, ((AV_RN32A(src) + AV_RN32A(ref)) >> 1) & 0x7F7F7F7FUL)
267 #define AVG_64(dst, src, ref) \
268 AV_WN64A(dst, ((AV_RN64A(src) + AV_RN64A(ref)) >> 1) & 0x7F7F7F7F7F7F7F7FULL)
277 a &= 0xFF00FF00FF00FF00ULL;
280 a &= 0x00FF00FF00FF00FFULL;
299 static inline void fill_64(uint8_t *dst,
const uint64_t pix, int32_t n,
302 for (; n > 0; dst += row_offset, n--)
318 #define BUFFER_PRECHECK \
319 if (*data_ptr >= last_ptr) \
320 return IV3_OUT_OF_DATA; \
322 #define RLE_BLOCK_COPY \
323 if (cell->mv_ptr || !skip_flag) \
324 copy_block4(dst, ref, row_offset, row_offset, 4 << v_zoom)
326 #define RLE_BLOCK_COPY_8 \
327 pix64 = AV_RN64A(ref);\
329 pix64 = replicate64(pix64);\
330 fill_64(dst + row_offset, pix64, 7, row_offset);\
331 AVG_64(dst, ref, dst + row_offset);\
333 fill_64(dst, pix64, 8, row_offset)
335 #define RLE_LINES_COPY \
336 copy_block4(dst, ref, row_offset, row_offset, num_lines << v_zoom)
338 #define RLE_LINES_COPY_M10 \
339 pix64 = AV_RN64A(ref);\
340 if (is_top_of_cell) {\
341 pix64 = replicate64(pix64);\
342 fill_64(dst + row_offset, pix64, (num_lines << 1) - 1, row_offset);\
343 AVG_64(dst, ref, dst + row_offset);\
345 fill_64(dst, pix64, num_lines << 1, row_offset)
347 #define APPLY_DELTA_4 \
348 AV_WN16A(dst + line_offset ,\
349 (AV_RN16A(ref ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
350 AV_WN16A(dst + line_offset + 2,\
351 (AV_RN16A(ref + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
353 if (is_top_of_cell && !cell->ypos) {\
354 AV_COPY32(dst, dst + row_offset);\
356 AVG_32(dst, ref, dst + row_offset);\
360 #define APPLY_DELTA_8 \
362 if (is_top_of_cell) { \
363 AV_WN32A(dst + row_offset , \
364 (replicate32(AV_RN32A(ref )) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
365 AV_WN32A(dst + row_offset + 4, \
366 (replicate32(AV_RN32A(ref + 4)) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
368 AV_WN32A(dst + row_offset , \
369 (AV_RN32A(ref ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
370 AV_WN32A(dst + row_offset + 4, \
371 (AV_RN32A(ref + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
376 if (is_top_of_cell && !cell->ypos) {\
377 AV_COPY64(dst, dst + row_offset);\
379 AVG_64(dst, ref, dst + row_offset);
382 #define APPLY_DELTA_1011_INTER \
385 (AV_RN32A(dst ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
387 (AV_RN32A(dst + 4 ) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
388 AV_WN32A(dst + row_offset , \
389 (AV_RN32A(dst + row_offset ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\
390 AV_WN32A(dst + row_offset + 4, \
391 (AV_RN32A(dst + row_offset + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\
394 (AV_RN16A(dst ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
396 (AV_RN16A(dst + 2 ) + delta_tab->deltas[dyad2]) & 0x7F7F);\
397 AV_WN16A(dst + row_offset , \
398 (AV_RN16A(dst + row_offset ) + delta_tab->deltas[dyad1]) & 0x7F7F);\
399 AV_WN16A(dst + row_offset + 2, \
400 (AV_RN16A(dst + row_offset + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\
405 int pitch,
int h_zoom,
int v_zoom,
int mode,
407 const uint8_t **data_ptr,
const uint8_t *last_ptr)
409 int x, y,
line, num_lines;
411 uint8_t code, *dst, *ref;
413 unsigned int dyad1, dyad2;
415 int skip_flag = 0, is_top_of_cell, is_first_row = 1;
416 int row_offset, blk_row_offset, line_offset;
419 blk_row_offset = (row_offset << (2 + v_zoom)) - (cell->
width << 2);
420 line_offset = v_zoom ? row_offset : 0;
425 for (y = 0; y < cell->
height; is_first_row = 0, y += 1 + v_zoom) {
426 for (x = 0; x < cell->
width; x += 1 + h_zoom) {
430 if (rle_blocks > 0) {
433 }
else if (mode == 10 && !cell->
mv_ptr) {
438 for (line = 0; line < 4;) {
440 is_top_of_cell = is_first_row && !line;
444 delta_tab = delta[line & 1];
446 delta_tab = delta[1];
448 code = bytestream_get_byte(data_ptr);
450 if (code < delta_tab->num_dyads) {
452 dyad1 = bytestream_get_byte(data_ptr);
454 if (dyad1 >= delta_tab->
num_dyads || dyad1 >= 248)
461 if (swap_quads[line & 1])
462 FFSWAP(
unsigned int, dyad1, dyad2);
466 }
else if (mode == 10 && !cell->
mv_ptr) {
482 num_lines = 257 - code - line;
487 }
else if (mode == 10 && !cell->
mv_ptr) {
493 code = bytestream_get_byte(data_ptr);
494 rle_blocks = (code & 0x1F) - 1;
495 if (code >= 64 || rle_blocks < 0)
497 skip_flag = code & 0x20;
498 num_lines = 4 - line;
499 if (mode >= 10 || (cell->
mv_ptr || !skip_flag)) {
502 }
else if (mode == 10 && !cell->
mv_ptr) {
518 }
else if (mode == 10 && !cell->
mv_ptr) {
529 ref += row_offset * (num_lines << v_zoom);
530 dst += row_offset * (num_lines << v_zoom);
535 block += 4 << h_zoom;
536 ref_block += 4 << h_zoom;
540 ref_block += blk_row_offset;
541 block += blk_row_offset;
562 const uint8_t *last_ptr)
564 int x, mv_x, mv_y, mode, vq_index, prim_indx, second_indx;
566 int offset, error = 0, swap_quads[2];
567 uint8_t code, *
block, *ref_block = 0;
569 const uint8_t *data_start = data_ptr;
574 vq_index = code & 0xF;
577 offset = (cell->
ypos << 2) * plane->
pitch + (cell->
xpos << 2);
581 ref_block = block - plane->
pitch;
582 }
else if (mode >= 10) {
590 offset += mv_y * plane->
pitch + mv_x;
597 if (mode == 1 || mode == 4) {
599 prim_indx = (code >> 4) + ctx->
cb_offset;
600 second_indx = (code & 0xF) + ctx->
cb_offset;
603 prim_indx = second_indx = vq_index;
606 if (prim_indx >= 24 || second_indx >= 24) {
607 av_log(avctx,
AV_LOG_ERROR,
"Invalid VQ table indexes! Primary: %d, secondary: %d!\n",
608 prim_indx, second_indx);
612 delta[0] = &
vq_tab[second_indx];
613 delta[1] = &
vq_tab[prim_indx];
614 swap_quads[0] = second_indx >= 16;
615 swap_quads[1] = prim_indx >= 16;
619 if (vq_index >= 8 && ref_block) {
620 for (x = 0; x < cell->
width << 2; x++)
621 ref_block[x] =
requant_tab[vq_index & 7][ref_block[x]];
631 if (mode >= 3 && cell->
mv_ptr) {
636 zoom_fac = mode >= 3;
638 mode, delta, swap_quads, &data_ptr, last_ptr);
642 if (mode == 10 && !cell->
mv_ptr) {
644 mode, delta, swap_quads, &data_ptr, last_ptr);
646 if (mode == 11 && !cell->
mv_ptr) {
651 zoom_fac = mode == 10;
653 zoom_fac, 1, mode, delta, swap_quads,
654 &data_ptr, last_ptr);
664 av_log(avctx,
AV_LOG_ERROR,
"Mode %d: RLE code %X is not allowed at the current line\n",
674 av_log(avctx,
AV_LOG_ERROR,
"Mode %d: unsupported RLE code: %X\n", mode, data_ptr[-1]);
681 return data_ptr - data_start;
694 #define SPLIT_CELL(size, new_size) (new_size) = ((size) > 2) ? ((((size) + 2) >> 2) << 1) : 1
696 #define UPDATE_BITPOS(n) \
697 ctx->skip_bits += (n); \
700 #define RESYNC_BITSTREAM \
701 if (ctx->need_resync && !(get_bits_count(&ctx->gb) & 7)) { \
702 skip_bits_long(&ctx->gb, ctx->skip_bits); \
703 ctx->skip_bits = 0; \
704 ctx->need_resync = 0; \
708 if (curr_cell.xpos + curr_cell.width > (plane->width >> 2) || \
709 curr_cell.ypos + curr_cell.height > (plane->height >> 2)) { \
710 av_log(avctx, AV_LOG_ERROR, "Invalid cell: x=%d, y=%d, w=%d, h=%d\n", \
711 curr_cell.xpos, curr_cell.ypos, curr_cell.width, curr_cell.height); \
712 return AVERROR_INVALIDDATA; \
718 const int depth,
const int strip_width)
728 curr_cell = *ref_cell;
736 if (curr_cell.
width > strip_width) {
738 curr_cell.
width = (curr_cell.
width <= (strip_width << 1) ? 1 : 2) * strip_width;
743 if (ref_cell->
width <= 0 || curr_cell.
width <= 0)
752 if (
parse_bintree(ctx, avctx, plane, code, &curr_cell, depth - 1, strip_width))
756 if (!curr_cell.
tree) {
777 if (!curr_cell.
tree) {
795 bytes_used =
decode_cell(ctx, avctx, plane, &curr_cell,
813 Plane *plane,
const uint8_t *
data, int32_t data_size,
817 unsigned num_vectors;
821 num_vectors = bytestream_get_le32(&data);
822 if (num_vectors > 256) {
824 "Read invalid number of motion vectors %d\n", num_vectors);
827 if (num_vectors * 2 >= data_size)
834 init_get_bits(&ctx->
gb, &data[num_vectors * 2], (data_size - num_vectors * 2) << 3);
841 curr_cell.
xpos = curr_cell.
ypos = 0;
851 #define OS_HDR_ID MKBETAG('F', 'R', 'M', 'H')
854 const uint8_t *buf,
int buf_size)
856 const uint8_t *buf_ptr = buf, *bs_hdr;
857 uint32_t frame_num, word2, check_sum, data_size;
858 uint32_t y_offset, u_offset, v_offset, starts[3], ends[3];
863 frame_num = bytestream_get_le32(&buf_ptr);
864 word2 = bytestream_get_le32(&buf_ptr);
865 check_sum = bytestream_get_le32(&buf_ptr);
866 data_size = bytestream_get_le32(&buf_ptr);
868 if ((frame_num ^ word2 ^ data_size ^
OS_HDR_ID) != check_sum) {
876 if (bytestream_get_le16(&buf_ptr) != 32) {
883 ctx->
data_size = (bytestream_get_le32(&buf_ptr) + 7) >> 3;
894 height = bytestream_get_le16(&buf_ptr);
895 width = bytestream_get_le16(&buf_ptr);
899 if (width != ctx->
width || height != ctx->
height) {
902 av_dlog(avctx,
"Frame dimensions changed!\n");
904 if (width < 16 || width > 640 ||
905 height < 16 || height > 480 ||
906 width & 3 || height & 3) {
908 "Invalid picture dimensions: %d x %d!\n", width, height);
921 y_offset = bytestream_get_le32(&buf_ptr);
922 v_offset = bytestream_get_le32(&buf_ptr);
923 u_offset = bytestream_get_le32(&buf_ptr);
927 starts[0] = y_offset;
928 starts[1] = v_offset;
929 starts[2] = u_offset;
931 for (j = 0; j < 3; j++) {
933 for (i = 2; i >= 0; i--)
934 if (starts[i] < ends[j] && starts[i] > starts[j])
950 ctx->
alt_quant = buf_ptr +
sizeof(uint32_t);
983 const uint8_t *src = plane->
pixels[buf_sel];
984 uint32_t pitch = plane->
pitch;
986 for (y = 0; y < plane->
height; y++) {
988 for (x = 0; x < plane->
width >> 2; x++) {
994 for (x <<= 2; x < plane->
width; x++)
995 *dst++ = *src++ << 1;
997 src += pitch - plane->
width;
998 dst += dst_pitch - plane->
width;
1026 const uint8_t *buf = avpkt->
data;
1027 int buf_size = avpkt->
size;