25#define BITSTREAM_READER_LE
164 }
else if (
mode == 0) {
181 const int *quant_matrix,
int *
skip,
int *dc_level)
183 const uint8_t *scantable =
s->permutated_scantable;
186 memset(
s->wblocks, 0,
s->wblocks_size);
188 for (
int i = 0;
i < 64;
i++) {
189 int16_t *
block =
s->wblocks + scantable[
i];
191 for (
int j = 0; j <
s->blocks_w;) {
198 for (
int k = 0; k < rskip; k++)
199 block[64 * k] = *dc_level * quant_matrix[0];
211 block[0] = (
i == 0 ? *dc_level :
level) * quant_matrix[
i];
222 const int *quant_matrix,
int *
skip,
225 const uint8_t *scantable =
s->permutated_scantable;
228 memset(
s->wblocks, 0,
s->wblocks_size);
229 memset(
s->map, 0,
s->map_size);
231 for (
int i = 0;
i < 64;
i++) {
232 int16_t *
block =
s->wblocks + scantable[
i];
234 for (
int j = 0; j <
s->blocks_w;) {
258 const int *quant_matrix,
int *
skip,
int *dc_level)
260 const uint8_t *scantable =
s->permutated_scantable;
261 const int offset =
s->plus ? 0 : 1024;
262 int16_t *
block =
s->block;
265 memset(
block, 0,
sizeof(
s->block));
275 block[scantable[0]] =
offset + *dc_level * quant_matrix[0];
277 for (
int i = 1;
i < 64;) {
301 int ret,
skip = 0, dc_level = 0;
302 const int offset =
s->plus ? 0 : 1024;
309 64 *
s->blocks_w *
sizeof(*
s->wblocks));
313 for (
int y = 0; y <
s->blocks_h; y++) {
318 for (
int x = 0; x <
s->blocks_w; x++) {
320 s->idsp.idct_put(
frame->data[plane] + (
s->blocks_h - 1 - y) * 8 *
frame->linesize[plane] + x * 8,
321 frame->linesize[plane],
s->wblocks + 64 * x);
325 for (
int y = 0; y <
s->blocks_h; y++) {
326 for (
int x = 0; x <
s->blocks_w; x++) {
331 s->idsp.idct_put(
frame->data[plane] + (
s->blocks_h - 1 - y) * 8 *
frame->linesize[plane] + x * 8,
332 frame->linesize[plane],
s->block);
347 const int *quant_matrix,
int *
skip,
350 const uint8_t *scantable =
s->permutated_scantable;
351 int16_t *
block =
s->block;
354 memset(
block, 0,
sizeof(
s->block));
356 for (
int i = 0;
i < 64;) {
387 64 *
s->blocks_w *
sizeof(*
s->wblocks));
392 s->blocks_w *
sizeof(*
s->map));
396 for (
int y = 0; y <
s->blocks_h; y++) {
401 for (
int x = 0; x <
s->blocks_w; x++) {
402 int shift = plane == 0;
404 int orig_mv_x =
s->mvectors[mvpos].x;
405 int mv_x =
s->mvectors[mvpos].x / (1 + !
shift);
406 int mv_y =
s->mvectors[mvpos].y / (1 + !
shift);
407 int h =
s->avctx->coded_height >> !
shift;
408 int w =
s->avctx->coded_width >> !
shift;
411 if (orig_mv_x >= -32) {
412 int src_y = (
s->blocks_h - 1 - y) * 8 - mv_y;
413 int src_x = x * 8 + mv_x;
414 if (src_y < 0 || src_y + 8 >
h ||
415 src_x < 0 || src_x + 8 >
w)
419 prev->
data[plane] + src_y * prev->
linesize[plane] + src_x,
422 s->idsp.idct(
s->wblocks + x * 64);
423 for (
int i = 0;
i < 64;
i++)
424 s->wblocks[
i + x * 64] = (
s->wblocks[
i + x * 64] + 1) & 0xFFFC;
425 s->idsp.add_pixels_clamped(&
s->wblocks[x*64],
frame->data[plane] + (
s->blocks_h - 1 - y) * 8 *
frame->linesize[plane] + x * 8,
426 frame->linesize[plane]);
429 s->idsp.idct_put(
frame->data[plane] + (
s->blocks_h - 1 - y) * 8 *
frame->linesize[plane] + x * 8,
430 frame->linesize[plane],
s->wblocks + x * 64);
434 }
else if (
s->flags & 2) {
435 for (
int y = 0; y <
s->blocks_h; y++) {
436 for (
int x = 0; x <
s->blocks_w; x++) {
437 int shift = plane == 0;
439 int orig_mv_x =
s->mvectors[mvpos].x;
440 int mv_x =
s->mvectors[mvpos].x / (1 + !
shift);
441 int mv_y =
s->mvectors[mvpos].y / (1 + !
shift);
442 int h =
s->avctx->coded_height >> !
shift;
443 int w =
s->avctx->coded_width >> !
shift;
450 if (orig_mv_x >= -32) {
451 int src_y = (
s->blocks_h - 1 - y) * 8 - mv_y;
452 int src_x = x * 8 + mv_x;
453 if (src_y < 0 || src_y + 8 >
h ||
454 src_x < 0 || src_x + 8 >
w)
458 prev->
data[plane] + src_y * prev->
linesize[plane] + src_x,
461 s->idsp.idct(
s->block);
462 for (
int i = 0;
i < 64;
i++)
463 s->block[
i] = (
s->block[
i] + 1) & 0xFFFC;
464 s->idsp.add_pixels_clamped(
s->block,
frame->data[plane] + (
s->blocks_h - 1 - y) * 8 *
frame->linesize[plane] + x * 8,
465 frame->linesize[plane]);
468 s->idsp.idct_put(
frame->data[plane] + (
s->blocks_h - 1 - y) * 8 *
frame->linesize[plane] + x * 8,
469 frame->linesize[plane],
s->block);
473 }
else if (
s->flags & 1) {
475 64 *
s->blocks_w *
sizeof(*
s->wblocks));
480 s->blocks_w *
sizeof(*
s->map));
484 for (
int y = 0; y <
s->blocks_h; y++) {
489 for (
int x = 0; x <
s->blocks_w; x++) {
492 s->idsp.idct_add(
frame->data[plane] + (
s->blocks_h - 1 - y) * 8 *
frame->linesize[plane] + x * 8,
493 frame->linesize[plane],
s->wblocks + 64 * x);
497 for (
int y = 0; y <
s->blocks_h; y++) {
498 for (
int x = 0; x <
s->blocks_w; x++) {
507 s->idsp.idct_add(
frame->data[plane] + (
s->blocks_h - 1 - y) * 8 *
frame->linesize[plane] + x * 8,
508 frame->linesize[plane],
s->block);
525 double f = 1.0 -
fabs(qscale);
527 if (!
s->key_frame && (
s->flags & 2)) {
529 for (
int i = 0;
i < 64;
i++) {
534 for (
int i = 0;
i < 64;
i++) {
535 luma[
i] =
FFMAX(1, 16 - qscale * 32);
541 for (
int i = 0;
i < 64;
i++) {
546 for (
int i = 0;
i < 64;
i++) {
553 for (
int i = 0;
i < 64;
i++) {
556 s->luma_quant_matrix[
i] = luma[
pos] * ((
pos / 8) & 1 ? -1 : 1);
557 s->chroma_quant_matrix[
i] =
chroma[
pos] * ((
pos / 8) & 1 ? -1 : 1);
564 uint8_t
r = 0,
g = 0,
b = 0;
569 for (
int y = 0; y < avctx->
height; y++) {
570 for (
int x = 0; x < avctx->
width; x++) {
571 dst[x*3+0] = bytestream2_get_byteu(gbyte) +
r;
573 dst[x*3+1] = bytestream2_get_byteu(gbyte) +
g;
575 dst[x*3+2] = bytestream2_get_byteu(gbyte) +
b;
585 uint8_t **
u, uint8_t **v,
586 int ylinesize,
int ulinesize,
int vlinesize,
588 int *nx,
int *ny,
int *np,
int w,
int h)
590 uint8_t *y0dst = *y0;
591 uint8_t *y1dst = *y1;
594 int x = *nx, y = *ny,
pos = *np;
597 y0dst[2*x+0] += fill[0];
598 y0dst[2*x+1] += fill[1];
599 y1dst[2*x+0] += fill[2];
600 y1dst[2*x+1] += fill[3];
602 }
else if (
pos == 1) {
611 y0dst -= 2*ylinesize;
612 y1dst -= 2*ylinesize;
616 y0dst[2*x+0] += fill[2];
617 y0dst[2*x+1] += fill[3];
619 }
else if (
pos == 2) {
620 y1dst[2*x+0] += fill[0];
621 y1dst[2*x+1] += fill[1];
630 y0dst -= 2*ylinesize;
631 y1dst -= 2*ylinesize;
652 int runlen, y = 0, x = 0;
657 code = bytestream2_peek_le32(gbyte);
658 runlen =
code & 0xFFFFFF;
660 if (
code >> 24 == 0x77) {
663 for (
int i = 0;
i < 4;
i++)
664 fill[
i] = bytestream2_get_byte(gbyte);
669 for (
int i = 0;
i < 4;
i++) {
672 if (x >=
frame->width * 3) {
676 if (y >=
frame->height)
682 for (
int i = 0;
i < 4;
i++)
683 fill[
i] = bytestream2_get_byte(gbyte);
685 for (
int i = 0;
i < 4;
i++) {
688 if (x >=
frame->width * 3) {
692 if (y >=
frame->height)
705 uint8_t *y1dst = y0dst -
frame->linesize[0];
706 uint8_t *udst =
frame->data[1] + ((avctx->
height >> 1) - 1) *
frame->linesize[1];
707 uint8_t *vdst =
frame->data[2] + ((avctx->
height >> 1) - 1) *
frame->linesize[2];
708 int runlen, y = 0, x = 0,
pos = 0;
713 code = bytestream2_peek_le32(gbyte);
714 runlen =
code & 0xFFFFFF;
716 if (
code >> 24 == 0x77) {
719 for (
int i = 0;
i < 4;
i++)
720 fill[
i] = bytestream2_get_byte(gbyte);
735 for (
int i = 0;
i < 4;
i++)
736 fill[
i] = bytestream2_get_byte(gbyte);
755 uint8_t *y1dst = y0dst -
frame->linesize[0];
756 uint8_t *udst =
frame->data[1] + ((avctx->
height >> 1) - 1) *
frame->linesize[1];
757 uint8_t *vdst =
frame->data[2] + ((avctx->
height >> 1) - 1) *
frame->linesize[2];
758 uint8_t ly0 = 0, ly1 = 0, ly2 = 0, ly3 = 0, lu = 0, lv = 0;
760 for (
int y = 0; y < avctx->
height / 2; y++) {
761 for (
int x = 0; x < avctx->
width / 2; x++) {
762 y0dst[x*2+0] = bytestream2_get_byte(gbyte) + ly0;
764 y0dst[x*2+1] = bytestream2_get_byte(gbyte) + ly1;
766 y1dst[x*2+0] = bytestream2_get_byte(gbyte) + ly2;
768 y1dst[x*2+1] = bytestream2_get_byte(gbyte) + ly3;
770 udst[x] = bytestream2_get_byte(gbyte) + lu;
772 vdst[x] = bytestream2_get_byte(gbyte) + lv;
776 y0dst -= 2*
frame->linesize[0];
777 y1dst -= 2*
frame->linesize[0];
778 udst -=
frame->linesize[1];
779 vdst -=
frame->linesize[2];
827 nb_mvs *
sizeof(*
s->mvectors));
832 (
s->size[0] +
s->size[1] +
s->size[2]))) < 0)
835 memset(
s->mvectors, 0,
sizeof(*
s->mvectors) * nb_mvs);
837 for (
int i = 0;
i < nb_mvs;
i++) {
845 for (
int i = 0;
i < nb_mvs;
i++) {
910 if (idx < 256 && idx >= 0) {
912 }
else if (idx >= 0) {
913 get_tree_codes(codes, nodes, nodes[idx].child[0], pfx + (0 << bitpos), bitpos + 1);
914 get_tree_codes(codes, nodes, nodes[idx].child[1], pfx + (1U << bitpos), bitpos + 1);
920 int zlcount = 0, curlen, idx, nindex, last, llast;
921 int blcounts[32] = { 0 };
927 for (
int i = 0;
i < 256;
i++) {
928 int bitlen = bitlens[
i];
929 int blcount = blcounts[bitlen];
931 zlcount += bitlen < 1;
932 syms[(bitlen << 8) + blcount] =
i;
936 for (
int i = 0;
i < 512;
i++) {
941 for (
int i = 0;
i < 256;
i++) {
942 node_idx[
i] = 257 +
i;
950 for (curlen = 1; curlen < 32; curlen++) {
951 if (blcounts[curlen] > 0) {
952 int max_zlcount = zlcount + blcounts[curlen];
954 for (
int i = 0; zlcount < 256 && zlcount < max_zlcount; zlcount++,
i++) {
955 int p = node_idx[nindex - 1 + 512];
956 int ch = syms[256 * curlen +
i];
961 if (nodes[p].child[0] == -1) {
962 nodes[p].
child[0] = ch;
964 nodes[p].
child[1] = ch;
976 p = node_idx[nindex - 1 + 512];
978 if (nodes[p].child[0] == -1) {
979 nodes[p].
child[0] = ch;
981 nodes[p].
child[1] = ch;
990 for (
int i = 0;
i < idx;
i++)
991 node_idx[512 +
i] = old_idx[
i];
1005 uint32_t new_codes[256];
1007 uint8_t symbols[256];
1008 uint32_t codes[256];
1015 for (
int i = 0;
i < 256;
i++) {
1017 bits[nb_codes] = bitlen[
i];
1018 codes[nb_codes] = new_codes[
i];
1019 symbols[nb_codes] =
i;
1045 if (
s->output_size > avctx->
width * avctx->
height * 9LL + 10000)
1058 for (
int i = 0;
i < count;
i++)
1061 for (
int i = 0;
i < 256;
i++) {
1066 for (
int i = 0;
i < 256;
i++)
1078 s->output[x++] =
val;
1091 unsigned compressed_size;
1100 header = bytestream2_get_le32(gbyte);
1101 s->fflags = bytestream2_get_le32(gbyte);
1102 s->bitstream_size =
s->fflags & 0x1FFFFFFF;
1105 if (avpkt->
size <
s->bitstream_size + 8)
1115 if (!
s->key_frame) {
1116 if (!
s->prev_frame->data[0]) {
1131 }
else if (!
s->dct) {
1137 w = bytestream2_get_le32(gbyte);
1138 h = bytestream2_get_le32(gbyte);
1139 if (
w == INT32_MIN ||
h == INT32_MIN)
1161 s->compression = bytestream2_get_le32(gbyte);
1162 if (
s->compression < 0 ||
s->compression > 100)
1165 for (
int i = 0;
i < 3;
i++)
1166 s->size[
i] = bytestream2_get_le32(gbyte);
1168 compressed_size =
s->output_size;
1171 compressed_size = avpkt->
size;
1174 if (
s->size[0] < 0 ||
s->size[1] < 0 ||
s->size[2] < 0 ||
1175 skip +
s->size[0] +
s->size[1] +
s->size[2] > compressed_size) {
1184 if (!
s->dct && !
s->rgb)
1186 else if (!
s->dct &&
s->rgb)
1192 s->prev_frame->height !=
frame->height)
1195 if (!(
s->flags & 2)) {
1203 }
else if (!
s->dct && !
s->rgb) {
1236 if (!
s->rgb && !
s->dct) {
1244 s->idsp.idct_permutation);
1267 s->mvectors_size = 0;
1269 s->wblocks_size = 0;
1271 s->padded_output_size = 0;
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static double val(void *priv, double ch)
static int decode_motion_vectors(AVCodecContext *avctx, GetBitContext *gb)
static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
static av_cold void decode_flush(AVCodecContext *avctx)
static int decode_inter(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame, AVFrame *prev)
static int decode_intra_plane(AGMContext *s, GetBitContext *gb, int size, const int *quant_matrix, AVFrame *frame, int plane)
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
static int decode_huffman2(AVCodecContext *avctx, int header, int size)
static av_cold int decode_close(AVCodecContext *avctx)
static void get_tree_codes(uint32_t *codes, Node *nodes, int idx, uint32_t pfx, int bitpos)
static int decode_runlen_rgb(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
static int make_new_tree(const uint8_t *bitlens, uint32_t *codes)
const FFCodec ff_agm_decoder
static av_cold int decode_init(AVCodecContext *avctx)
static int decode_intra_block(AGMContext *s, GetBitContext *gb, const int *quant_matrix, int *skip, int *dc_level)
static int decode_inter_block(AGMContext *s, GetBitContext *gb, const int *quant_matrix, int *skip, int *map)
static int decode_raw_intra_rgb(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
static int decode_inter_blocks(AGMContext *s, GetBitContext *gb, const int *quant_matrix, int *skip, int *map)
static int decode_intra_blocks(AGMContext *s, GetBitContext *gb, const int *quant_matrix, int *skip, int *dc_level)
static int decode_runlen(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
static int decode_raw_intra(AVCodecContext *avctx, GetByteContext *gbyte, AVFrame *frame)
static av_always_inline int fill_pixels(uint8_t **y0, uint8_t **y1, uint8_t **u, uint8_t **v, int ylinesize, int ulinesize, int vlinesize, uint8_t *fill, int *nx, int *ny, int *np, int w, int h)
static int build_huff(const uint8_t *bitlen, VLC *vlc)
static int read_code(GetBitContext *gb, int *oskip, int *level, int *map, int mode)
static int decode_inter_plane(AGMContext *s, GetBitContext *gb, int size, const int *quant_matrix, AVFrame *frame, AVFrame *prev, int plane)
static void compute_quant_matrix(AGMContext *s, double qscale)
static av_cold void close(AVCodecParserContext *s)
Libavcodec external API header.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
#define i(width, name, range_min, range_max)
#define FF_CODEC_CAP_EXPORTS_CROPPING
The decoder sets the cropping fields in the output frames manually.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
static void copy_block8(uint8_t *dst, const uint8_t *src, ptrdiff_t dstStride, ptrdiff_t srcStride, int h)
static __device__ float fabs(float a)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int ff_set_dimensions(AVCodecContext *s, int width, int height)
int(* init)(AVBSFContext *ctx)
static const uint8_t bits[8]
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
static int get_bits_left(GetBitContext *gb)
static void skip_bits(GetBitContext *s, int n)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static const uint8_t * align_get_bits(GetBitContext *s)
static int get_bits_count(const GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
@ AV_PICTURE_TYPE_I
Intra.
@ AV_PICTURE_TYPE_P
Predicted.
const VDPAUPixFmtMap * map
const uint8_t ff_mjpeg_std_luminance_quant_tbl[64]
const uint8_t ff_mjpeg_std_chrominance_quant_tbl[64]
static av_cold int decode_init(AVCodecContext *avctx)
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
static av_cold int decode_close(AVCodecContext *avctx)
static int shift(int a, int b)
#define u(width, name, range_min, range_max)
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64], const uint8_t permutation[64])
Macro definitions for various function/variable attributes.
#define MKTAG(a, b, c, d)
const uint8_t ff_zigzag_direct[64]
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
static const uint8_t header[24]
int chroma_quant_matrix[64]
unsigned padded_output_size
uint8_t permutated_scantable[64]
int luma_quant_matrix[64]
Describe the class of an AVClass context structure.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
int coded_width
Bitstream width / height, may be different from width/height e.g.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
This structure stores compressed data.
int flags
A combination of AV_PKT_FLAG values.
static double cb(void *priv, double x, double y)
int ff_vlc_init_sparse(VLC *vlc, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Build VLC decoding tables suitable for use with get_vlc2().
void ff_vlc_free(VLC *vlc)