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_size, chroma_size;
170 ptrdiff_t luma_pitch, chroma_pitch;
172 luma_width =
FFALIGN(luma_width , 2);
173 luma_height =
FFALIGN(luma_height, 2);
175 if (luma_width < 16 || luma_width > 640 ||
176 luma_height < 16 || luma_height > 480 ||
177 luma_width & 3 || luma_height & 3) {
179 luma_width, luma_height);
183 ctx->
width = luma_width ;
184 ctx->
height = luma_height;
186 chroma_width =
FFALIGN(luma_width >> 2, 4);
187 chroma_height =
FFALIGN(luma_height >> 2, 4);
189 luma_pitch =
FFALIGN(luma_width, 16);
190 chroma_pitch =
FFALIGN(chroma_width, 16);
194 luma_size = luma_pitch * (luma_height + 1);
198 chroma_size = chroma_pitch * (chroma_height + 1);
201 for (p = 0; p < 3; p++) {
202 ctx->
planes[p].
pitch = !p ? luma_pitch : chroma_pitch;
203 ctx->
planes[p].
width = !p ? luma_width : chroma_width;
204 ctx->
planes[p].
height = !p ? luma_height : chroma_height;
238 int h,
w, mv_x, mv_y,
offset, offset_dst;
242 offset_dst = (cell->
ypos << 2) * plane->
pitch + (cell->
xpos << 2);
251 if ((cell->
ypos << 2) + mv_y < -1 || (cell->
xpos << 2) + mv_x < 0 ||
255 "Motion vectors point out of the frame.\n");
259 offset = offset_dst + mv_y * plane->
pitch + mv_x;
264 for (w = cell->
width; w > 0;) {
266 if (!((cell->
xpos << 2) & 15) && w >= 4) {
267 for (; w >= 4; src += 16, dst += 16, w -= 4)
272 if (!((cell->
xpos << 2) & 7) && w >= 2) {
290 #define AVG_32(dst, src, ref) \ 291 AV_WN32A(dst, ((AV_RN32(src) + AV_RN32(ref)) >> 1) & 0x7F7F7F7FUL) 293 #define AVG_64(dst, src, ref) \ 294 AV_WN64A(dst, ((AV_RN64(src) + AV_RN64(ref)) >> 1) & 0x7F7F7F7F7F7F7F7FULL) 303 a &= 0xFF00FF00FF00FF00ULL;
306 a &= 0x00FF00FF00FF00FFULL;
328 for (; n > 0; dst += row_offset, n--)
344 #define BUFFER_PRECHECK \ 345 if (*data_ptr >= last_ptr) \ 346 return IV3_OUT_OF_DATA; \ 348 #define RLE_BLOCK_COPY \ 349 if (cell->mv_ptr || !skip_flag) \ 350 copy_block4(dst, ref, row_offset, row_offset, 4 << v_zoom) 352 #define RLE_BLOCK_COPY_8 \ 353 pix64 = AV_RN64(ref);\ 355 pix64 = replicate64(pix64);\ 356 fill_64(dst + row_offset, pix64, 7, row_offset);\ 357 AVG_64(dst, ref, dst + row_offset);\ 359 fill_64(dst, pix64, 8, row_offset) 361 #define RLE_LINES_COPY \ 362 copy_block4(dst, ref, row_offset, row_offset, num_lines << v_zoom) 364 #define RLE_LINES_COPY_M10 \ 365 pix64 = AV_RN64(ref);\ 366 if (is_top_of_cell) {\ 367 pix64 = replicate64(pix64);\ 368 fill_64(dst + row_offset, pix64, (num_lines << 1) - 1, row_offset);\ 369 AVG_64(dst, ref, dst + row_offset);\ 371 fill_64(dst, pix64, num_lines << 1, row_offset) 373 #define APPLY_DELTA_4 \ 374 AV_WN16A(dst + line_offset ,\ 375 (AV_RN16(ref ) + delta_tab->deltas[dyad1]) & 0x7F7F);\ 376 AV_WN16A(dst + line_offset + 2,\ 377 (AV_RN16(ref + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\ 379 if (is_top_of_cell && !cell->ypos) {\ 380 AV_COPY32U(dst, dst + row_offset);\ 382 AVG_32(dst, ref, dst + row_offset);\ 386 #define APPLY_DELTA_8 \ 388 if (is_top_of_cell) { \ 389 AV_WN32A(dst + row_offset , \ 390 (replicate32(AV_RN32(ref )) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\ 391 AV_WN32A(dst + row_offset + 4, \ 392 (replicate32(AV_RN32(ref + 4)) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\ 394 AV_WN32A(dst + row_offset , \ 395 (AV_RN32(ref ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\ 396 AV_WN32A(dst + row_offset + 4, \ 397 (AV_RN32(ref + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\ 402 if (is_top_of_cell && !cell->ypos) {\ 403 AV_COPY64U(dst, dst + row_offset);\ 405 AVG_64(dst, ref, dst + row_offset); 408 #define APPLY_DELTA_1011_INTER \ 411 (AV_RN32(dst ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\ 413 (AV_RN32(dst + 4 ) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\ 414 AV_WN32A(dst + row_offset , \ 415 (AV_RN32(dst + row_offset ) + delta_tab->deltas_m10[dyad1]) & 0x7F7F7F7F);\ 416 AV_WN32A(dst + row_offset + 4, \ 417 (AV_RN32(dst + row_offset + 4) + delta_tab->deltas_m10[dyad2]) & 0x7F7F7F7F);\ 420 (AV_RN16(dst ) + delta_tab->deltas[dyad1]) & 0x7F7F);\ 422 (AV_RN16(dst + 2 ) + delta_tab->deltas[dyad2]) & 0x7F7F);\ 423 AV_WN16A(dst + row_offset , \ 424 (AV_RN16(dst + row_offset ) + delta_tab->deltas[dyad1]) & 0x7F7F);\ 425 AV_WN16A(dst + row_offset + 2, \ 426 (AV_RN16(dst + row_offset + 2) + delta_tab->deltas[dyad2]) & 0x7F7F);\ 432 ptrdiff_t row_offset,
int h_zoom,
int v_zoom,
int mode,
436 int x, y,
line, num_lines;
440 unsigned int dyad1, dyad2;
442 int skip_flag = 0, is_top_of_cell, is_first_row = 1;
443 int blk_row_offset, line_offset;
445 blk_row_offset = (row_offset << (2 + v_zoom)) - (cell->
width << 2);
446 line_offset = v_zoom ? row_offset : 0;
451 for (y = 0; y < cell->
height; is_first_row = 0, y += 1 + v_zoom) {
452 for (x = 0; x < cell->
width; x += 1 + h_zoom) {
456 if (rle_blocks > 0) {
459 }
else if (mode == 10 && !cell->
mv_ptr) {
464 for (line = 0; line < 4;) {
466 is_top_of_cell = is_first_row && !line;
470 delta_tab = delta[line & 1];
472 delta_tab = delta[1];
474 code = bytestream_get_byte(data_ptr);
476 if (code < delta_tab->num_dyads) {
478 dyad1 = bytestream_get_byte(data_ptr);
480 if (dyad1 >= delta_tab->
num_dyads || dyad1 >= 248)
487 if (swap_quads[line & 1])
488 FFSWAP(
unsigned int, dyad1, dyad2);
492 }
else if (mode == 10 && !cell->
mv_ptr) {
508 num_lines = 257 - code - line;
513 }
else if (mode == 10 && !cell->
mv_ptr) {
519 code = bytestream_get_byte(data_ptr);
520 rle_blocks = (code & 0x1F) - 1;
521 if (code >= 64 || rle_blocks < 0)
523 skip_flag = code & 0x20;
524 num_lines = 4 - line;
525 if (mode >= 10 || (cell->
mv_ptr || !skip_flag)) {
528 }
else if (mode == 10 && !cell->
mv_ptr) {
544 }
else if (mode == 10 && !cell->
mv_ptr) {
555 ref += row_offset * (num_lines << v_zoom);
556 dst += row_offset * (num_lines << v_zoom);
561 block += 4 << h_zoom;
562 ref_block += 4 << h_zoom;
566 ref_block += blk_row_offset;
567 block += blk_row_offset;
590 int x, mv_x, mv_y,
mode, vq_index, prim_indx, second_indx;
595 const uint8_t *data_start = data_ptr;
600 vq_index = code & 0xF;
603 offset = (cell->
ypos << 2) * plane->
pitch + (cell->
xpos << 2);
608 ref_block = block - plane->
pitch;
609 }
else if (mode >= 10) {
621 if ((cell->
ypos << 2) + mv_y < -1 || (cell->
xpos << 2) + mv_x < 0 ||
625 "Motion vectors point out of the frame.\n");
629 offset += mv_y * plane->
pitch + mv_x;
636 if (mode == 1 || mode == 4) {
638 prim_indx = (code >> 4) + ctx->
cb_offset;
639 second_indx = (code & 0xF) + ctx->
cb_offset;
642 prim_indx = second_indx = vq_index;
645 if (prim_indx >= 24 || second_indx >= 24) {
646 av_log(avctx,
AV_LOG_ERROR,
"Invalid VQ table indexes! Primary: %d, secondary: %d!\n",
647 prim_indx, second_indx);
651 delta[0] = &
vq_tab[second_indx];
652 delta[1] = &
vq_tab[prim_indx];
653 swap_quads[0] = second_indx >= 16;
654 swap_quads[1] = prim_indx >= 16;
658 if (vq_index >= 8 && ref_block) {
659 for (x = 0; x < cell->
width << 2; x++)
660 ref_block[x] =
requant_tab[vq_index & 7][ref_block[x] & 127];
670 if (mode >= 3 && cell->
mv_ptr) {
675 zoom_fac = mode >= 3;
677 0, zoom_fac, mode, delta, swap_quads,
678 &data_ptr, last_ptr);
682 if (mode == 10 && !cell->
mv_ptr) {
684 1, 1, mode, delta, swap_quads,
685 &data_ptr, last_ptr);
687 if (mode == 11 && !cell->
mv_ptr) {
692 zoom_fac = mode == 10;
694 zoom_fac, 1, mode, delta, swap_quads,
695 &data_ptr, last_ptr);
705 av_log(avctx,
AV_LOG_ERROR,
"Mode %d: RLE code %X is not allowed at the current line\n",
715 av_log(avctx,
AV_LOG_ERROR,
"Mode %d: unsupported RLE code: %X\n", mode, data_ptr[-1]);
722 return data_ptr - data_start;
735 #define SPLIT_CELL(size, new_size) (new_size) = ((size) > 2) ? ((((size) + 2) >> 2) << 1) : 1 737 #define UPDATE_BITPOS(n) \ 738 ctx->skip_bits += (n); \ 741 #define RESYNC_BITSTREAM \ 742 if (ctx->need_resync && !(get_bits_count(&ctx->gb) & 7)) { \ 743 skip_bits_long(&ctx->gb, ctx->skip_bits); \ 744 ctx->skip_bits = 0; \ 745 ctx->need_resync = 0; \ 749 if (curr_cell.xpos + curr_cell.width > (plane->width >> 2) || \ 750 curr_cell.ypos + curr_cell.height > (plane->height >> 2)) { \ 751 av_log(avctx, AV_LOG_ERROR, "Invalid cell: x=%d, y=%d, w=%d, h=%d\n", \ 752 curr_cell.xpos, curr_cell.ypos, curr_cell.width, curr_cell.height); \ 753 return AVERROR_INVALIDDATA; \ 759 const int depth,
const int strip_width)
769 curr_cell = *ref_cell;
777 if (curr_cell.
width > strip_width) {
779 curr_cell.
width = (curr_cell.
width <= (strip_width << 1) ? 1 : 2) * strip_width;
784 if (ref_cell->
width <= 0 || curr_cell.
width <= 0)
793 if (
parse_bintree(ctx, avctx, plane, code, &curr_cell, depth - 1, strip_width))
797 if (!curr_cell.
tree) {
819 if (!curr_cell.
tree) {
841 bytes_used =
decode_cell(ctx, avctx, plane, &curr_cell,
863 unsigned num_vectors;
867 num_vectors = bytestream_get_le32(&data); data_size -= 4;
868 if (num_vectors > 256) {
870 "Read invalid number of motion vectors %d\n", num_vectors);
873 if (num_vectors * 2 > data_size)
880 init_get_bits(&ctx->
gb, &data[num_vectors * 2], (data_size - num_vectors * 2) << 3);
887 curr_cell.
xpos = curr_cell.
ypos = 0;
897 #define OS_HDR_ID MKBETAG('F', 'R', 'M', 'H') 900 const uint8_t *buf,
int buf_size)
904 uint32_t frame_num, word2, check_sum, data_size;
905 int y_offset, u_offset, v_offset;
906 uint32_t starts[3], ends[3];
913 frame_num = bytestream2_get_le32(&gb);
914 word2 = bytestream2_get_le32(&gb);
915 check_sum = bytestream2_get_le32(&gb);
916 data_size = bytestream2_get_le32(&gb);
918 if ((frame_num ^ word2 ^ data_size ^
OS_HDR_ID) != check_sum) {
926 if (bytestream2_get_le16(&gb) != 32) {
933 ctx->
data_size = (bytestream2_get_le32(&gb) + 7) >> 3;
934 ctx->
cb_offset = bytestream2_get_byte(&gb);
943 height = bytestream2_get_le16(&gb);
944 width = bytestream2_get_le16(&gb);
948 if (width != ctx->
width || height != ctx->
height) {
951 ff_dlog(avctx,
"Frame dimensions changed!\n");
953 if (width < 16 || width > 640 ||
954 height < 16 || height > 480 ||
955 width & 3 || height & 3) {
957 "Invalid picture dimensions: %d x %d!\n", width, height);
967 y_offset = bytestream2_get_le32(&gb);
968 v_offset = bytestream2_get_le32(&gb);
969 u_offset = bytestream2_get_le32(&gb);
974 starts[0] = y_offset;
975 starts[1] = v_offset;
976 starts[2] = u_offset;
978 for (j = 0; j < 3; j++) {
980 for (i = 2; i >= 0; i--)
981 if (starts[i] < ends[j] && starts[i] > starts[j])
988 if (
FFMIN3(y_offset, v_offset, u_offset) < 0 ||
990 FFMIN3(y_offset, v_offset, u_offset) < gb.
buffer - bs_hdr + 16 ||
1031 ptrdiff_t dst_pitch,
int dst_height)
1038 for (y = 0; y < dst_height; y++) {
1040 for (x = 0; x < plane->
width >> 2; x++) {
1046 for (x <<= 2; x < plane->
width; x++)
1047 *dst++ = *src++ << 1;
1049 src += pitch - plane->
width;
1050 dst += dst_pitch - plane->
width;
1075 int buf_size = avpkt->
size;
1121 (avctx->
height + 3) >> 2);
1124 (avctx->
height + 3) >> 2);
static int decode_cell(Indeo3DecodeContext *ctx, AVCodecContext *avctx, Plane *plane, Cell *cell, const uint8_t *data_ptr, const uint8_t *last_ptr)
Decode a vector-quantized cell.
discard all frames except keyframes
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
#define BS_8BIT_PEL
8-bit pixel bitdepth indicator
const uint8_t * next_cell_data
static int decode_plane(Indeo3DecodeContext *ctx, AVCodecContext *avctx, Plane *plane, const uint8_t *data, int32_t data_size, int32_t strip_width)
same as RLE_ESC_FA + do the same with next block
int16_t xpos
cell coordinates in 4x4 blocks
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
const uint8_t * y_data_ptr
static av_cold int init(AVCodecContext *avctx)
#define avpriv_request_sample(...)
int16_t height
cell height in 4x4 blocks
uint8_t quad_exp
log2 of four-pixel deltas
uint8_t num_dyads
number of two-pixel deltas
apply null delta to all lines up to the 2nd line
const uint8_t * v_data_ptr
#define RLE_LINES_COPY_M10
#define BS_BUFFER
indicates which of two frame buffers should be used
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static av_cold void free_frame_buffers(Indeo3DecodeContext *ctx)
static void error(const char *err)
uint32_t frame_num
current frame number (zero-based)
uint8_t buf_sel
active frame buffer: 0 - primary, 1 -secondary
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
apply null delta to N blocks / skip N blocks
enum AVDiscard skip_frame
Skip decoding for selected frames.
#define BS_KEYFRAME
intra frame indicator
static av_cold int decode_close(AVCodecContext *avctx)
static void output_plane(const Plane *plane, int buf_sel, uint8_t *dst, ptrdiff_t dst_pitch, int dst_height)
Convert and output the current plane.
int data_size
size of the frame data in bytes
apply null delta to all lines up to the 3rd line
static int get_bits_count(const GetBitContext *s)
bitstream reader API header.
static int decode_frame_headers(Indeo3DecodeContext *ctx, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
static uint64_t replicate64(uint64_t a)
const uint8_t * last_byte
static int get_bits_left(GetBitContext *gb)
#define i(width, name, range_min, range_max)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
uint16_t frame_flags
frame properties
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
uint8_t cb_offset
needed for selecting VQ tables
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int flags
AV_CODEC_FLAG_*.
static const vqEntry vq_tab[24]
const char * name
Name of the codec implementation.
static const uint8_t offset[127][2]
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
static const struct @315 planes[]
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
#define BS_NONREF
nonref (discardable) frame indicator
uint8_t * pixels[2]
pointer to the actual pixel data of the buffers above
In the ELBG jargon, a cell is the set of points that are closest to a codebook entry.
int width
picture width / height.
#define BS_MV_X_HALF
horizontal mv halfpel resolution indicator
const uint8_t * alt_quant
secondary VQ table set for the modes 1 and 4
static uint32_t replicate32(uint32_t a)
const int8_t * mv_ptr
ptr to the motion vector if any
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
static void fill_64(uint8_t *dst, const uint64_t pix, int32_t n, int32_t row_offset)
Libavcodec external API header.
static int copy_cell(Indeo3DecodeContext *ctx, Plane *plane, Cell *cell)
Copy pixels of the cell(x + mv_x, y + mv_y) from the previous frame into the cell(x, y) in the current frame.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
same as RLE_ESC_FD + do the same with next block
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
const int8_t * mc_vectors
#define SPLIT_CELL(size, new_size)
int16_t width
cell width in 4x4 blocks
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static av_cold int decode_init(AVCodecContext *avctx)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static int parse_bintree(Indeo3DecodeContext *ctx, AVCodecContext *avctx, Plane *plane, int code, Cell *ref_cell, const int depth, const int strip_width)
static av_cold int allocate_frame_buffers(Indeo3DecodeContext *ctx, AVCodecContext *avctx, int luma_width, int luma_height)
static av_cold void build_requant_tab(void)
discard all non reference
INTRA: skip block, INTER: copy data from reference.
static uint8_t requant_tab[8][128]
common internal api header.
static int ref[MAX_W *MAX_W]
apply null delta to all remaining lines of this block
unsigned num_vectors
number of motion vectors in mc_vectors
#define BS_MV_Y_HALF
vertical mv halfpel resolution indicator
static int decode_cell_data(Indeo3DecodeContext *ctx, Cell *cell, uint8_t *block, uint8_t *ref_block, ptrdiff_t row_offset, int h_zoom, int v_zoom, int mode, const vqEntry *delta[2], int swap_quads[2], const uint8_t **data_ptr, const uint8_t *last_ptr)
AVCodec ff_indeo3_decoder
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
const uint8_t * u_data_ptr
uint8_t tree
tree id: 0- MC tree, 1 - VQ tree
#define FFSWAP(type, a, b)
This structure stores compressed data.
mode
Use these values in ebur128_init (or'ed).
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
#define APPLY_DELTA_1011_INTER