1 /*
2 * Electronic Arts Madcow Video Decoder
3 * Copyright (c) 2007-2009 Peter Ross
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Electronic Arts Madcow Video Decoder
25 * @author Peter Ross <pross@xvid.org>
26 *
27 * @see technical details at
28 * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_MAD
29 */
30
42
43 #define EA_PREAMBLE_SIZE 8
44 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
45 #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD p-frame */
46 #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
47
63
65 {
75
79
80 return 0;
81 }
82
83 static inline void comp(
unsigned char *dst,
int dst_stride,
84 unsigned char *
src,
int src_stride,
int add)
85 {
86 int j, i;
87 for (j=0; j<8; j++)
88 for (i=0; i<8; i++)
89 dst[j*dst_stride + i] = av_clip_uint8(src[j*src_stride + i] + add);
90 }
91
93 int mb_x, int mb_y,
94 int j, int mv_x, int mv_y, int add)
95 {
96 if (j < 4) {
99 return;
100 comp(frame->
data[0] + (mb_y*16 + ((j&2)<<2))*frame->
linesize[0] + mb_x*16 + ((j&1)<<3),
108 return;
113 }
114 }
115
117 int mb_x, int mb_y, int j)
118 {
119 if (j < 4) {
121 frame->
data[0] + (mb_y*16 + ((j&2)<<2))*frame->
linesize[0] + mb_x*16 + ((j&1)<<3),
126 frame->
data[index] + (mb_y*8)*frame->
linesize[index] + mb_x*8,
128 }
129 }
130
132 {
137
138 block[0] = (128 +
get_sbits(&s->
gb, 8)) * quant_matrix[0];
139
140 /* The RL decoder is derived from mpeg1_decode_block_intra;
141 Escaped level and run values a decoded differently */
142 i = 0;
143 {
145 /* now quantify & encode AC coefficients */
146 for (;;) {
149
150 if (level == 127) {
151 break;
152 } else if (level != 0) {
154 if (i > 63) {
156 "ac-tex damaged at %d %d\n", s->
mb_x, s->
mb_y);
157 return -1;
158 }
159 j = scantable[i];
160 level = (level*quant_matrix[j]) >> 4;
161 level = (level-1)|1;
164 } else {
165 /* escape */
168
171
173 if (i > 63) {
175 "ac-tex damaged at %d %d\n", s->
mb_x, s->
mb_y);
176 return -1;
177 }
178 j = scantable[i];
179 if (level < 0) {
181 level = (level*quant_matrix[j]) >> 4;
182 level = (level-1)|1;
184 } else {
185 level = (level*quant_matrix[j]) >> 4;
186 level = (level-1)|1;
187 }
188 }
189
191 }
193 }
194 return 0;
195 }
196
198 {
202 value = -17;
204 }
206 }
207
209 {
210 int mv_map = 0;
212 int j;
213
214 if (inter) {
216 if (v < 2) {
220 }
221 }
222
223 for (j=0; j<6; j++) {
224 if (mv_map & (1<<j)) { // mv_x and mv_y are guarded by mv_map
228 } else {
231 return -1;
233 }
234 }
235 return 0;
236 }
237
239 {
240 int i;
241
243 for (i=1; i<64; i++)
245 }
246
248 void *
data,
int *got_frame,
250 {
252 int buf_size = avpkt->
size;
257 int chunk_type;
258 int inter, ret;
259
261
262 chunk_type = bytestream2_get_le32(&gb);
265
267 bytestream2_get_le16(&gb), 1000, 1<<30);
268
269 width = bytestream2_get_le16(&gb);
270 height = bytestream2_get_le16(&gb);
274
278 }
279
280 if (width < 16 || height < 16) {
283 }
284
285 if (avctx->
width != width || avctx->
height != height) {
290 return ret;
291 }
292
294 return ret;
295
299 if (ret < 0)
300 return ret;
307 }
308
317
322
323 *got_frame = 1;
324
328 return ret;
329 }
330
331 return buf_size;
332 }
333
335 {
339 return 0;
340 }
341
352 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
void(* clear_block)(int16_t *block)
uint16_t quant_matrix[64]
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
av_cold void ff_mpeg12_init_vlcs(void)
static void comp_block(MadContext *t, AVFrame *frame, int mb_x, int mb_y, int j, int mv_x, int mv_y, int add)
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.
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
static av_cold int init(AVCodecContext *avctx)
#define DECLARE_ALIGNED(n, t, v)
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)
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...
static int get_sbits(GetBitContext *s, int n)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
bitstream reader API header.
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
static void calc_quant_matrix(MadContext *s, int qscale)
#define UPDATE_CACHE(name, gb)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
int flags
AV_CODEC_FLAG_*.
const char * name
Name of the codec implementation.
static av_cold int decode_end(AVCodecContext *avctx)
static int decode_block_intra(MadContext *s, int16_t *block)
static const uint8_t offset[127][2]
#define CLOSE_READER(name, gb)
#define SKIP_BITS(name, gb, num)
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation, enum idct_permutation_type perm_type)
int width
picture width / height.
GLsizei GLboolean const GLfloat * value
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define LAST_SKIP_BITS(name, gb, num)
uint8_t idct_permutation[64]
IDCT input permutation.
RL_VLC_ELEM * rl_vlc[32]
decoding only
#define SHOW_UBITS(name, gb, num)
static int decode210(GetBitContext *gb)
static av_always_inline int bytestream2_tell(GetByteContext *g)
Libavcodec external API header.
static void idct_put(MadContext *t, AVFrame *frame, int16_t *block, int mb_x, int mb_y, int j)
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
const uint16_t ff_inv_aanscales[64]
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define OPEN_READER(name, gb)
static int decode_motion(GetBitContext *gb)
static unsigned int get_bits1(GetBitContext *s)
BYTE int const BYTE int int int height
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
const uint16_t ff_mpeg1_default_intra_matrix[256]
const uint8_t ff_zigzag_direct[64]
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
AAN (Arai, Agui and Nakajima) (I)DCT tables.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
void ff_ea_idct_put_c(uint8_t *dest, int linesize, int16_t *block)
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
#define SHOW_SBITS(name, gb, num)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal api header.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
static int decode_mb(MadContext *s, AVFrame *frame, int inter)
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
unsigned int bitstream_buf_size
This structure stores compressed data.
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static av_cold int decode_init(AVCodecContext *avctx)