2 * Electronic Arts CMV Video Decoder
3 * Copyright (c) 2007-2008 Peter Ross
5 * This file is part of FFmpeg.
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.
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.
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
24 * Electronic Arts CMV Video Decoder
25 * by Peter Ross (pross@xvid.org)
27 * Technical details here:
28 * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_CMV
31 #include "libavutil/intreadwrite.h"
32 #include "libavcore/imgutils.h"
35 typedef struct CmvContext {
36 AVCodecContext *avctx;
37 AVFrame frame; ///< current
38 AVFrame last_frame; ///< last
39 AVFrame last2_frame; ///< second-last
41 unsigned int palette[AVPALETTE_COUNT];
44 static av_cold int cmv_decode_init(AVCodecContext *avctx){
45 CmvContext *s = avctx->priv_data;
47 avctx->pix_fmt = PIX_FMT_PAL8;
51 static void cmv_decode_intra(CmvContext * s, const uint8_t *buf, const uint8_t *buf_end){
52 unsigned char *dst = s->frame.data[0];
55 for (i=0; i < s->avctx->height && buf+s->avctx->width<=buf_end; i++) {
56 memcpy(dst, buf, s->avctx->width);
57 dst += s->frame.linesize[0];
58 buf += s->avctx->width;
62 static void cmv_motcomp(unsigned char *dst, int dst_stride,
63 const unsigned char *src, int src_stride,
65 int xoffset, int yoffset,
66 int width, int height){
72 if (i+xoffset>=0 && i+xoffset<width &&
73 j+yoffset>=0 && j+yoffset<height) {
74 dst[j*dst_stride + i] = src[(j+yoffset)*src_stride + i+xoffset];
76 dst[j*dst_stride + i] = 0;
81 static void cmv_decode_inter(CmvContext * s, const uint8_t *buf, const uint8_t *buf_end){
82 const uint8_t *raw = buf + (s->avctx->width*s->avctx->height/16);
86 for(y=0; y<s->avctx->height/4; y++)
87 for(x=0; x<s->avctx->width/4 && buf+i<buf_end; x++) {
89 unsigned char *dst = s->frame.data[0] + (y*4)*s->frame.linesize[0] + x*4;
90 if (raw+16<buf_end && *raw==0xFF) { /* intra */
93 memcpy(dst+s->frame.linesize[0], raw+4, 4);
94 memcpy(dst+2*s->frame.linesize[0], raw+8, 4);
95 memcpy(dst+3*s->frame.linesize[0], raw+12, 4);
97 }else if(raw<buf_end) { /* inter using second-last frame as reference */
98 int xoffset = (*raw & 0xF) - 7;
99 int yoffset = ((*raw >> 4)) - 7;
100 cmv_motcomp(s->frame.data[0], s->frame.linesize[0],
101 s->last2_frame.data[0], s->last2_frame.linesize[0],
102 x*4, y*4, xoffset, yoffset, s->avctx->width, s->avctx->height);
105 }else{ /* inter using last frame as reference */
106 int xoffset = (buf[i] & 0xF) - 7;
107 int yoffset = ((buf[i] >> 4)) - 7;
108 cmv_motcomp(s->frame.data[0], s->frame.linesize[0],
109 s->last_frame.data[0], s->last_frame.linesize[0],
110 x*4, y*4, xoffset, yoffset, s->avctx->width, s->avctx->height);
116 static void cmv_process_header(CmvContext *s, const uint8_t *buf, const uint8_t *buf_end)
118 int pal_start, pal_count, i;
120 if(buf+16>=buf_end) {
121 av_log(s->avctx, AV_LOG_WARNING, "truncated header\n");
125 s->width = AV_RL16(&buf[4]);
126 s->height = AV_RL16(&buf[6]);
127 if (s->avctx->width!=s->width || s->avctx->height!=s->height)
128 avcodec_set_dimensions(s->avctx, s->width, s->height);
130 s->avctx->time_base.num = 1;
131 s->avctx->time_base.den = AV_RL16(&buf[10]);
133 pal_start = AV_RL16(&buf[12]);
134 pal_count = AV_RL16(&buf[14]);
137 for (i=pal_start; i<pal_start+pal_count && i<AVPALETTE_COUNT && buf+2<buf_end; i++) {
138 s->palette[i] = AV_RB24(buf);
143 #define EA_PREAMBLE_SIZE 8
144 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h')
146 static int cmv_decode_frame(AVCodecContext *avctx,
147 void *data, int *data_size,
150 const uint8_t *buf = avpkt->data;
151 int buf_size = avpkt->size;
152 CmvContext *s = avctx->priv_data;
153 const uint8_t *buf_end = buf + buf_size;
155 if (AV_RL32(buf)==MVIh_TAG||AV_RB32(buf)==MVIh_TAG) {
156 cmv_process_header(s, buf+EA_PREAMBLE_SIZE, buf_end);
160 if (av_check_image_size(s->width, s->height, 0, s->avctx))
164 if (s->last2_frame.data[0])
165 avctx->release_buffer(avctx, &s->last2_frame);
166 FFSWAP(AVFrame, s->last_frame, s->last2_frame);
167 FFSWAP(AVFrame, s->frame, s->last_frame);
169 s->frame.reference = 1;
170 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
171 if (avctx->get_buffer(avctx, &s->frame)<0) {
172 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
176 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
178 buf += EA_PREAMBLE_SIZE;
179 if ((buf[0]&1)) { // subtype
180 cmv_decode_inter(s, buf+2, buf_end);
181 s->frame.key_frame = 0;
182 s->frame.pict_type = FF_P_TYPE;
184 s->frame.key_frame = 1;
185 s->frame.pict_type = FF_I_TYPE;
186 cmv_decode_intra(s, buf+2, buf_end);
189 *data_size = sizeof(AVFrame);
190 *(AVFrame*)data = s->frame;
195 static av_cold int cmv_decode_end(AVCodecContext *avctx){
196 CmvContext *s = avctx->priv_data;
197 if (s->frame.data[0])
198 s->avctx->release_buffer(avctx, &s->frame);
199 if (s->last_frame.data[0])
200 s->avctx->release_buffer(avctx, &s->last_frame);
201 if (s->last2_frame.data[0])
202 s->avctx->release_buffer(avctx, &s->last2_frame);
207 AVCodec eacmv_decoder = {
217 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts CMV video"),