2008-07-08 16:22:23 +03:00
|
|
|
/*
|
|
|
|
* Electronic Arts CMV Video Decoder
|
|
|
|
* Copyright (c) 2007-2008 Peter Ross
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2008-07-08 16:22:23 +03:00
|
|
|
* Electronic Arts CMV Video Decoder
|
2010-01-09 05:31:13 +02:00
|
|
|
* by Peter Ross (pross@xvid.org)
|
2008-07-08 16:22:23 +03:00
|
|
|
*
|
|
|
|
* Technical details here:
|
|
|
|
* http://wiki.multimedia.cx/index.php?title=Electronic_Arts_CMV
|
|
|
|
*/
|
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/common.h"
|
2009-01-12 00:19:48 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2008-07-08 16:22:23 +03:00
|
|
|
#include "avcodec.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2022-08-24 21:28:16 +02:00
|
|
|
#include "decode.h"
|
2008-07-08 16:22:23 +03:00
|
|
|
|
|
|
|
typedef struct CmvContext {
|
|
|
|
AVCodecContext *avctx;
|
2012-11-21 23:34:46 +03:00
|
|
|
AVFrame *last_frame; ///< last
|
|
|
|
AVFrame *last2_frame; ///< second-last
|
2008-07-08 16:22:23 +03:00
|
|
|
int width, height;
|
|
|
|
unsigned int palette[AVPALETTE_COUNT];
|
|
|
|
} CmvContext;
|
|
|
|
|
|
|
|
static av_cold int cmv_decode_init(AVCodecContext *avctx){
|
|
|
|
CmvContext *s = avctx->priv_data;
|
2011-05-02 03:15:15 +03:00
|
|
|
|
2008-07-08 16:22:23 +03:00
|
|
|
s->avctx = avctx;
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2012-11-21 23:34:46 +03:00
|
|
|
|
|
|
|
s->last_frame = av_frame_alloc();
|
|
|
|
s->last2_frame = av_frame_alloc();
|
2020-08-29 08:26:03 +02:00
|
|
|
if (!s->last_frame || !s->last2_frame)
|
2012-11-21 23:34:46 +03:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2008-07-08 16:22:23 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
static void cmv_decode_intra(CmvContext * s, AVFrame *frame,
|
|
|
|
const uint8_t *buf, const uint8_t *buf_end)
|
|
|
|
{
|
|
|
|
unsigned char *dst = frame->data[0];
|
2008-07-08 16:22:23 +03:00
|
|
|
int i;
|
|
|
|
|
2011-10-01 01:44:59 +03:00
|
|
|
for (i=0; i < s->avctx->height && buf_end - buf >= s->avctx->width; i++) {
|
2008-07-08 16:22:23 +03:00
|
|
|
memcpy(dst, buf, s->avctx->width);
|
2012-11-21 23:34:46 +03:00
|
|
|
dst += frame->linesize[0];
|
2008-07-08 16:22:23 +03:00
|
|
|
buf += s->avctx->width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 17:08:15 +02:00
|
|
|
static void cmv_motcomp(unsigned char *dst, ptrdiff_t dst_stride,
|
|
|
|
const unsigned char *src, ptrdiff_t src_stride,
|
2008-07-08 16:22:23 +03:00
|
|
|
int x, int y,
|
|
|
|
int xoffset, int yoffset,
|
|
|
|
int width, int height){
|
|
|
|
int i,j;
|
|
|
|
|
|
|
|
for(j=y;j<y+4;j++)
|
|
|
|
for(i=x;i<x+4;i++)
|
|
|
|
{
|
|
|
|
if (i+xoffset>=0 && i+xoffset<width &&
|
|
|
|
j+yoffset>=0 && j+yoffset<height) {
|
|
|
|
dst[j*dst_stride + i] = src[(j+yoffset)*src_stride + i+xoffset];
|
|
|
|
}else{
|
|
|
|
dst[j*dst_stride + i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
static void cmv_decode_inter(CmvContext *s, AVFrame *frame, const uint8_t *buf,
|
|
|
|
const uint8_t *buf_end)
|
|
|
|
{
|
2008-07-08 16:22:23 +03:00
|
|
|
const uint8_t *raw = buf + (s->avctx->width*s->avctx->height/16);
|
|
|
|
int x,y,i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for(y=0; y<s->avctx->height/4; y++)
|
2011-10-01 01:44:59 +03:00
|
|
|
for(x=0; x<s->avctx->width/4 && buf_end - buf > i; x++) {
|
2008-07-08 16:22:23 +03:00
|
|
|
if (buf[i]==0xFF) {
|
2012-11-21 23:34:46 +03:00
|
|
|
unsigned char *dst = frame->data[0] + (y*4)*frame->linesize[0] + x*4;
|
2008-07-08 16:22:23 +03:00
|
|
|
if (raw+16<buf_end && *raw==0xFF) { /* intra */
|
|
|
|
raw++;
|
|
|
|
memcpy(dst, raw, 4);
|
2012-11-21 23:34:46 +03:00
|
|
|
memcpy(dst + frame->linesize[0], raw+4, 4);
|
|
|
|
memcpy(dst + 2 * frame->linesize[0], raw+8, 4);
|
|
|
|
memcpy(dst + 3 * frame->linesize[0], raw+12, 4);
|
2008-07-08 16:22:23 +03:00
|
|
|
raw+=16;
|
|
|
|
}else if(raw<buf_end) { /* inter using second-last frame as reference */
|
|
|
|
int xoffset = (*raw & 0xF) - 7;
|
|
|
|
int yoffset = ((*raw >> 4)) - 7;
|
2012-11-21 23:34:46 +03:00
|
|
|
if (s->last2_frame->data[0])
|
|
|
|
cmv_motcomp(frame->data[0], frame->linesize[0],
|
|
|
|
s->last2_frame->data[0], s->last2_frame->linesize[0],
|
2011-01-09 10:30:23 +02:00
|
|
|
x*4, y*4, xoffset, yoffset, s->avctx->width, s->avctx->height);
|
2008-07-08 16:22:23 +03:00
|
|
|
raw++;
|
|
|
|
}
|
|
|
|
}else{ /* inter using last frame as reference */
|
|
|
|
int xoffset = (buf[i] & 0xF) - 7;
|
|
|
|
int yoffset = ((buf[i] >> 4)) - 7;
|
2013-03-12 05:20:18 +03:00
|
|
|
if (s->last_frame->data[0])
|
|
|
|
cmv_motcomp(frame->data[0], frame->linesize[0],
|
2013-09-16 21:07:30 +03:00
|
|
|
s->last_frame->data[0], s->last_frame->linesize[0],
|
|
|
|
x*4, y*4, xoffset, yoffset, s->avctx->width, s->avctx->height);
|
2008-07-08 16:22:23 +03:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-27 12:02:26 +03:00
|
|
|
static int cmv_process_header(CmvContext *s, const uint8_t *buf, const uint8_t *buf_end)
|
2008-07-08 16:22:23 +03:00
|
|
|
{
|
2013-12-12 09:31:26 +03:00
|
|
|
int pal_start, pal_count, i, ret, fps;
|
2008-07-08 16:22:23 +03:00
|
|
|
|
2011-10-01 01:44:59 +03:00
|
|
|
if(buf_end - buf < 16) {
|
2008-07-08 16:22:23 +03:00
|
|
|
av_log(s->avctx, AV_LOG_WARNING, "truncated header\n");
|
2013-10-27 12:02:26 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2008-07-08 16:22:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s->width = AV_RL16(&buf[4]);
|
|
|
|
s->height = AV_RL16(&buf[6]);
|
2013-10-27 12:02:26 +03:00
|
|
|
|
2013-11-28 12:54:35 +03:00
|
|
|
if (s->width != s->avctx->width ||
|
|
|
|
s->height != s->avctx->height) {
|
2013-03-12 05:20:18 +03:00
|
|
|
av_frame_unref(s->last_frame);
|
|
|
|
av_frame_unref(s->last2_frame);
|
2013-01-25 02:41:16 +03:00
|
|
|
}
|
2008-07-08 16:22:23 +03:00
|
|
|
|
2013-10-27 12:02:26 +03:00
|
|
|
ret = ff_set_dimensions(s->avctx, s->width, s->height);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2008-07-08 16:22:23 +03:00
|
|
|
|
2013-12-12 09:31:26 +03:00
|
|
|
fps = AV_RL16(&buf[10]);
|
|
|
|
if (fps > 0)
|
2014-04-04 13:47:44 +03:00
|
|
|
s->avctx->framerate = (AVRational){ fps, 1 };
|
2008-07-08 16:22:23 +03:00
|
|
|
|
|
|
|
pal_start = AV_RL16(&buf[12]);
|
|
|
|
pal_count = AV_RL16(&buf[14]);
|
|
|
|
|
|
|
|
buf += 16;
|
2011-10-01 01:44:59 +03:00
|
|
|
for (i=pal_start; i<pal_start+pal_count && i<AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
|
2012-10-14 04:17:13 +03:00
|
|
|
s->palette[i] = 0xFFU << 24 | AV_RB24(buf);
|
2008-07-08 16:22:23 +03:00
|
|
|
buf += 3;
|
|
|
|
}
|
2013-10-27 12:02:26 +03:00
|
|
|
|
|
|
|
return 0;
|
2008-07-08 16:22:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define EA_PREAMBLE_SIZE 8
|
|
|
|
#define MVIh_TAG MKTAG('M', 'V', 'I', 'h')
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
static int cmv_decode_frame(AVCodecContext *avctx, AVFrame *frame,
|
|
|
|
int *got_frame, AVPacket *avpkt)
|
2008-07-08 16:22:23 +03:00
|
|
|
{
|
2009-04-07 18:59:50 +03:00
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
2008-07-08 16:22:23 +03:00
|
|
|
CmvContext *s = avctx->priv_data;
|
|
|
|
const uint8_t *buf_end = buf + buf_size;
|
2012-11-21 23:34:46 +03:00
|
|
|
int ret;
|
2008-07-08 16:22:23 +03:00
|
|
|
|
2011-10-01 01:45:01 +03:00
|
|
|
if (buf_end - buf < EA_PREAMBLE_SIZE)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2008-07-08 16:22:23 +03:00
|
|
|
if (AV_RL32(buf)==MVIh_TAG||AV_RB32(buf)==MVIh_TAG) {
|
2012-01-22 15:17:53 +03:00
|
|
|
unsigned size = AV_RL32(buf + 4);
|
2013-10-27 12:02:26 +03:00
|
|
|
ret = cmv_process_header(s, buf+EA_PREAMBLE_SIZE, buf_end);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-01-22 15:17:53 +03:00
|
|
|
if (size > buf_end - buf - EA_PREAMBLE_SIZE)
|
2018-12-31 19:59:49 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-01-22 15:17:53 +03:00
|
|
|
buf += size;
|
2008-07-08 16:22:23 +03:00
|
|
|
}
|
|
|
|
|
2018-12-31 19:59:49 +02:00
|
|
|
if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
|
|
|
|
return ret;
|
2008-07-08 16:22:23 +03:00
|
|
|
|
2024-09-19 23:57:31 +02:00
|
|
|
buf += EA_PREAMBLE_SIZE;
|
|
|
|
if (!(buf[0]&1) && buf_end - buf < s->width * s->height * (int64_t)(100 - s->avctx->discard_damaged_percentage) / 100)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
lavc: factorize ff_{thread_,re,}get_buffer error messages.
Coccinelle profile used:
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_get_buffer(ctx, f, flags)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str;
@@
-if ((r = ff_reget_buffer(ctx, f)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_reget_buffer(ctx, f)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0)
+ return r;
...along with some manual patches for the remaining ones.
2013-03-12 10:41:53 +03:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
|
2012-11-21 23:34:46 +03:00
|
|
|
return ret;
|
2008-07-08 16:22:23 +03:00
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
|
2008-07-08 16:22:23 +03:00
|
|
|
|
|
|
|
if ((buf[0]&1)) { // subtype
|
2012-11-21 23:34:46 +03:00
|
|
|
cmv_decode_inter(s, frame, buf+2, buf_end);
|
2023-04-12 18:58:54 +02:00
|
|
|
frame->flags &= ~AV_FRAME_FLAG_KEY;
|
2012-11-21 23:34:46 +03:00
|
|
|
frame->pict_type = AV_PICTURE_TYPE_P;
|
2008-07-08 16:22:23 +03:00
|
|
|
}else{
|
2023-04-12 18:58:54 +02:00
|
|
|
frame->flags |= AV_FRAME_FLAG_KEY;
|
2012-11-21 23:34:46 +03:00
|
|
|
frame->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
cmv_decode_intra(s, frame, buf+2, buf_end);
|
2008-07-08 16:22:23 +03:00
|
|
|
}
|
|
|
|
|
2023-09-07 12:02:26 +02:00
|
|
|
FFSWAP(AVFrame*, s->last2_frame, s->last_frame);
|
|
|
|
if ((ret = av_frame_replace(s->last_frame, frame)) < 0)
|
2012-11-21 23:34:46 +03:00
|
|
|
return ret;
|
|
|
|
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2008-07-08 16:22:23 +03:00
|
|
|
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int cmv_decode_end(AVCodecContext *avctx){
|
|
|
|
CmvContext *s = avctx->priv_data;
|
2012-11-21 23:34:46 +03:00
|
|
|
|
|
|
|
av_frame_free(&s->last_frame);
|
|
|
|
av_frame_free(&s->last2_frame);
|
2008-07-08 16:22:23 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_eacmv_decoder = {
|
|
|
|
.p.name = "eacmv",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("Electronic Arts CMV video"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_CMV,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(CmvContext),
|
|
|
|
.init = cmv_decode_init,
|
|
|
|
.close = cmv_decode_end,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(cmv_decode_frame),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1,
|
2022-07-10 00:05:45 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2008-07-08 16:22:23 +03:00
|
|
|
};
|