summaryrefslogtreecommitdiff
path: root/libavcodec/escape130.c
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2013-05-18 14:27:07 +0200
committerMichael Niedermayer <michaelni@gmx.at>2013-05-18 14:31:01 +0200
commited0aed09e8a19d255963c21c7aeda18cee3d9422 (patch)
tree59457517f96fdcd372f73da491372314a3d158d6 /libavcodec/escape130.c
parent849bb62c2c9afdb1e81f44870645a6990c4ce1fc (diff)
parent350914fc8024612f614c0a4f5c8ba5da2d9988e7 (diff)
Merge remote-tracking branch 'qatar/master'
* qatar/master: Escape 130 (RPL) decoder Conflicts: libavcodec/Makefile libavcodec/allcodecs.c libavcodec/codec_desc.c libavcodec/escape130.c libavcodec/version.h Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavcodec/escape130.c')
-rw-r--r--libavcodec/escape130.c408
1 files changed, 224 insertions, 184 deletions
diff --git a/libavcodec/escape130.c b/libavcodec/escape130.c
index d24af79cbe..003cd1be51 100644
--- a/libavcodec/escape130.c
+++ b/libavcodec/escape130.c
@@ -1,5 +1,5 @@
/*
- * Escape 130 Video Decoder
+ * Escape 130 video decoder
* Copyright (C) 2008 Eli Friedman (eli.friedman <at> gmail.com)
*
* This file is part of FFmpeg.
@@ -19,35 +19,134 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include "libavutil/attributes.h"
+#include "libavutil/mem.h"
#include "avcodec.h"
-
#define BITSTREAM_READER_LE
#include "get_bits.h"
#include "internal.h"
-
typedef struct Escape130Context {
- AVFrame frame;
- uint8_t *bases;
+ uint8_t *old_y_avg;
+
+ uint8_t *new_y, *old_y;
+ uint8_t *new_u, *old_u;
+ uint8_t *new_v, *old_v;
+
+ uint8_t *buf1, *buf2;
+ int linesize[3];
} Escape130Context;
-/**
- * Initialize the decoder
- * @param avctx decoder context
- * @return 0 success, negative on error
- */
+static const uint8_t offset_table[] = { 2, 4, 10, 20 };
+static const int8_t sign_table[64][4] = {
+ { 0, 0, 0, 0 },
+ { -1, 1, 0, 0 },
+ { 1, -1, 0, 0 },
+ { -1, 0, 1, 0 },
+ { -1, 1, 1, 0 },
+ { 0, -1, 1, 0 },
+ { 1, -1, 1, 0 },
+ { -1, -1, 1, 0 },
+ { 1, 0, -1, 0 },
+ { 0, 1, -1, 0 },
+ { 1, 1, -1, 0 },
+ { -1, 1, -1, 0 },
+ { 1, -1, -1, 0 },
+ { -1, 0, 0, 1 },
+ { -1, 1, 0, 1 },
+ { 0, -1, 0, 1 },
+
+ { 0, 0, 0, 0 },
+ { 1, -1, 0, 1 },
+ { -1, -1, 0, 1 },
+ { -1, 0, 1, 1 },
+ { -1, 1, 1, 1 },
+ { 0, -1, 1, 1 },
+ { 1, -1, 1, 1 },
+ { -1, -1, 1, 1 },
+ { 0, 0, -1, 1 },
+ { 1, 0, -1, 1 },
+ { -1, 0, -1, 1 },
+ { 0, 1, -1, 1 },
+ { 1, 1, -1, 1 },
+ { -1, 1, -1, 1 },
+ { 0, -1, -1, 1 },
+ { 1, -1, -1, 1 },
+
+ { 0, 0, 0, 0 },
+ { -1, -1, -1, 1 },
+ { 1, 0, 0, -1 },
+ { 0, 1, 0, -1 },
+ { 1, 1, 0, -1 },
+ { -1, 1, 0, -1 },
+ { 1, -1, 0, -1 },
+ { 0, 0, 1, -1 },
+ { 1, 0, 1, -1 },
+ { -1, 0, 1, -1 },
+ { 0, 1, 1, -1 },
+ { 1, 1, 1, -1 },
+ { -1, 1, 1, -1 },
+ { 0, -1, 1, -1 },
+ { 1, -1, 1, -1 },
+ { -1, -1, 1, -1 },
+
+ { 0, 0, 0, 0 },
+ { 1, 0, -1, -1 },
+ { 0, 1, -1, -1 },
+ { 1, 1, -1, -1 },
+ { -1, 1, -1, -1 },
+ { 1, -1, -1, -1 }
+};
+
+static const int8_t luma_adjust[] = { -4, -3, -2, -1, 1, 2, 3, 4 };
+
+static const int8_t chroma_adjust[2][8] = {
+ { 1, 1, 0, -1, -1, -1, 0, 1 },
+ { 0, 1, 1, 1, 0, -1, -1, -1 }
+};
+
+const uint8_t chroma_vals[] = {
+ 20, 28, 36, 44, 52, 60, 68, 76,
+ 84, 92, 100, 106, 112, 116, 120, 124,
+ 128, 132, 136, 140, 144, 150, 156, 164,
+ 172, 180, 188, 196, 204, 212, 220, 228
+};
+
static av_cold int escape130_decode_init(AVCodecContext *avctx)
{
Escape130Context *s = avctx->priv_data;
avctx->pix_fmt = AV_PIX_FMT_YUV420P;
- avcodec_get_frame_defaults(&s->frame);
- if((avctx->width&1) || (avctx->height&1)){
- av_log(avctx, AV_LOG_ERROR, "Dimensions are not a multiple of the block size\n");
- return AVERROR(EINVAL);
+ if ((avctx->width & 1) || (avctx->height & 1)) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Dimensions should be a multiple of two.\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ s->old_y_avg = av_malloc(avctx->width * avctx->height / 4);
+ s->buf1 = av_malloc(avctx->width * avctx->height * 3 / 2);
+ s->buf2 = av_malloc(avctx->width * avctx->height * 3 / 2);
+ if (!s->old_y_avg || !s->buf1 || !s->buf2) {
+ av_freep(&s->old_y_avg);
+ av_freep(&s->buf1);
+ av_freep(&s->buf2);
+ av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
+ return AVERROR(ENOMEM);
}
- s->bases= av_malloc(avctx->width * avctx->height /4);
+ s->linesize[0] = avctx->width;
+ s->linesize[1] =
+ s->linesize[2] = avctx->width / 2;
+
+ s->new_y = s->buf1;
+ s->new_u = s->new_y + avctx->width * avctx->height;
+ s->new_v = s->new_u + avctx->width * avctx->height / 4;
+ s->old_y = s->buf2;
+ s->old_u = s->old_y + avctx->width * avctx->height;
+ s->old_v = s->old_u + avctx->width * avctx->height / 4;
+ memset(s->old_y, 0, avctx->width * avctx->height);
+ memset(s->old_u, 0x10, avctx->width * avctx->height / 4);
+ memset(s->old_v, 0x10, avctx->width * avctx->height / 4);
return 0;
}
@@ -56,17 +155,17 @@ static av_cold int escape130_decode_close(AVCodecContext *avctx)
{
Escape130Context *s = avctx->priv_data;
- av_frame_unref(&s->frame);
-
- av_freep(&s->bases);
+ av_freep(&s->old_y_avg);
+ av_freep(&s->buf1);
+ av_freep(&s->buf2);
return 0;
}
-static unsigned decode_skip_count(GetBitContext* gb) {
- unsigned value;
- // This function reads a maximum of 27 bits,
- // which is within the padding space
+static int decode_skip_count(GetBitContext* gb)
+{
+ int value;
+
if (get_bits_left(gb) < 1+3)
return -1;
@@ -89,165 +188,87 @@ static unsigned decode_skip_count(GetBitContext* gb) {
return -1;
}
-/**
- * Decode a single frame
- * @param avctx decoder context
- * @param data decoded frame
- * @param got_frame have decoded frame
- * @param buf input buffer
- * @param buf_size input buffer size
- * @return 0 success, -1 on error
- */
-static int escape130_decode_frame(AVCodecContext *avctx,
- void *data, int *got_frame,
- AVPacket *avpkt)
+static int escape130_decode_frame(AVCodecContext *avctx, void *data,
+ int *got_frame, AVPacket *avpkt)
{
- const uint8_t *buf = avpkt->data;
- int buf_size = avpkt->size;
+ const uint8_t *buf = avpkt->data;
+ int buf_size = avpkt->size;
Escape130Context *s = avctx->priv_data;
-
+ AVFrame *pic = data;
GetBitContext gb;
- unsigned i;
int ret;
uint8_t *old_y, *old_cb, *old_cr,
*new_y, *new_cb, *new_cr;
+ uint8_t *dstY, *dstU, *dstV;
unsigned old_y_stride, old_cb_stride, old_cr_stride,
new_y_stride, new_cb_stride, new_cr_stride;
unsigned total_blocks = avctx->width * avctx->height / 4,
- block_index, row_index = 0;
- unsigned y[4] = {0}, cb = 16, cr = 16;
- unsigned skip = -1;
- unsigned y_base = 0;
- uint8_t *yb= s->bases;
-
- AVFrame *frame = data;
-
- init_get_bits(&gb, buf, buf_size * 8);
-
- if (get_bits_left(&gb) < 128)
- return -1;
-
- // Header; no useful information in here
- skip_bits_long(&gb, 128);
+ block_index, block_x = 0;
+ unsigned y[4] = { 0 }, cb = 0x10, cr = 0x10;
+ int skip = -1, y_avg = 0, i, j;
+ uint8_t *ya = s->old_y_avg;
+
+ // first 16 bytes are header; no useful information in here
+ if (buf_size <= 16) {
+ av_log(avctx, AV_LOG_ERROR, "Insufficient frame data\n");
+ return AVERROR_INVALIDDATA;
+ }
- if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
+ if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
return ret;
- new_y = frame->data[0];
- new_cb = frame->data[1];
- new_cr = frame->data[2];
- new_y_stride = frame->linesize[0];
- new_cb_stride = frame->linesize[1];
- new_cr_stride = frame->linesize[2];
- old_y = s->frame.data[0];
- old_cb = s->frame.data[1];
- old_cr = s->frame.data[2];
- old_y_stride = s->frame.linesize[0];
- old_cb_stride = s->frame.linesize[1];
- old_cr_stride = s->frame.linesize[2];
-
- av_log(avctx, AV_LOG_DEBUG,
- "Strides: %i, %i\n",
- new_y_stride, new_cb_stride);
+ init_get_bits(&gb, buf + 16, (buf_size - 16) * 8);
+
+ new_y = s->new_y;
+ new_cb = s->new_u;
+ new_cr = s->new_v;
+ new_y_stride = s->linesize[0];
+ new_cb_stride = s->linesize[1];
+ new_cr_stride = s->linesize[2];
+ old_y = s->old_y;
+ old_cb = s->old_u;
+ old_cr = s->old_v;
+ old_y_stride = s->linesize[0];
+ old_cb_stride = s->linesize[1];
+ old_cr_stride = s->linesize[2];
for (block_index = 0; block_index < total_blocks; block_index++) {
// Note that this call will make us skip the rest of the blocks
- // if the frame prematurely ends
+ // if the frame ends prematurely.
if (skip == -1)
skip = decode_skip_count(&gb);
+ if (skip == -1) {
+ av_log(avctx, AV_LOG_ERROR, "Error decoding skip value\n");
+ return AVERROR_INVALIDDATA;
+ }
if (skip) {
- if (old_y) {
- y[0] = old_y[0] / 4;
- y[1] = old_y[1] / 4;
- y[2] = old_y[old_y_stride] / 4;
- y[3] = old_y[old_y_stride+1] / 4;
- y_base= yb[0];
- cb = old_cb[0] / 8;
- cr = old_cr[0] / 8;
- } else {
- y_base=y[0] = y[1] = y[2] = y[3] = 0;
- cb = cr = 16;
- }
+ y[0] = old_y[0];
+ y[1] = old_y[1];
+ y[2] = old_y[old_y_stride];
+ y[3] = old_y[old_y_stride + 1];
+ y_avg = ya[0];
+ cb = old_cb[0];
+ cr = old_cr[0];
} else {
if (get_bits1(&gb)) {
- static const uint8_t offset_table[] = {2, 4, 10, 20};
- static const int8_t sign_table[64][4] =
- { {0, 0, 0, 0},
- {-1, 1, 0, 0},
- {1, -1, 0, 0},
- {-1, 0, 1, 0},
- {-1, 1, 1, 0},
- {0, -1, 1, 0},
- {1, -1, 1, 0},
- {-1, -1, 1, 0},
- {1, 0, -1, 0},
- {0, 1, -1, 0},
- {1, 1, -1, 0},
- {-1, 1, -1, 0},
- {1, -1, -1, 0},
- {-1, 0, 0, 1},
- {-1, 1, 0, 1},
- {0, -1, 0, 1},
-
- {0, 0, 0, 0},
- {1, -1, 0, 1},
- {-1, -1, 0, 1},
- {-1, 0, 1, 1},
- {-1, 1, 1, 1},
- {0, -1, 1, 1},
- {1, -1, 1, 1},
- {-1, -1, 1, 1},
- {0, 0, -1, 1},
- {1, 0, -1, 1},
- {-1, 0, -1, 1},
- {0, 1, -1, 1},
- {1, 1, -1, 1},
- {-1, 1, -1, 1},
- {0, -1, -1, 1},
- {1, -1, -1, 1},
-
- {0, 0, 0, 0},
- {-1, -1, -1, 1},
- {1, 0, 0, -1},
- {0, 1, 0, -1},
- {1, 1, 0, -1},
- {-1, 1, 0, -1},
- {1, -1, 0, -1},
- {0, 0, 1, -1},
- {1, 0, 1, -1},
- {-1, 0, 1, -1},
- {0, 1, 1, -1},
- {1, 1, 1, -1},
- {-1, 1, 1, -1},
- {0, -1, 1, -1},
- {1, -1, 1, -1},
- {-1, -1, 1, -1},
-
- {0, 0, 0, 0},
- {1, 0, -1, -1},
- {0, 1, -1, -1},
- {1, 1, -1, -1},
- {-1, 1, -1, -1},
- {1, -1, -1, -1} };
- unsigned sign_selector = get_bits(&gb, 6);
+ unsigned sign_selector = get_bits(&gb, 6);
unsigned difference_selector = get_bits(&gb, 2);
- y_base = 2 * get_bits(&gb, 5);
+ y_avg = 2 * get_bits(&gb, 5);
for (i = 0; i < 4; i++) {
- y[i] = av_clip((int)y_base + offset_table[difference_selector] *
- sign_table[sign_selector][i], 0, 63);
+ y[i] = av_clip(y_avg + offset_table[difference_selector] *
+ sign_table[sign_selector][i], 0, 63);
}
} else if (get_bits1(&gb)) {
if (get_bits1(&gb)) {
- y_base = get_bits(&gb, 6);
+ y_avg = get_bits(&gb, 6);
} else {
unsigned adjust_index = get_bits(&gb, 3);
- static const int8_t adjust[] = {-4, -3, -2, -1, 1, 2, 3, 4};
- y_base = (y_base + adjust[adjust_index]) & 63;
+ y_avg = (y_avg + luma_adjust[adjust_index]) & 63;
}
for (i = 0; i < 4; i++)
- y[i] = y_base;
+ y[i] = y_avg;
}
if (get_bits1(&gb)) {
@@ -256,56 +277,75 @@ static int escape130_decode_frame(AVCodecContext *avctx,
cr = get_bits(&gb, 5);
} else {
unsigned adjust_index = get_bits(&gb, 3);
- static const int8_t adjust[2][8] =
- { { 1, 1, 0, -1, -1, -1, 0, 1 },
- { 0, 1, 1, 1, 0, -1, -1, -1 } };
- cb = (cb + adjust[0][adjust_index]) & 31;
- cr = (cr + adjust[1][adjust_index]) & 31;
+ cb = (cb + chroma_adjust[0][adjust_index]) & 31;
+ cr = (cr + chroma_adjust[1][adjust_index]) & 31;
}
}
}
- *yb++= y_base;
-
- new_y[0] = y[0] * 4;
- new_y[1] = y[1] * 4;
- new_y[new_y_stride] = y[2] * 4;
- new_y[new_y_stride + 1] = y[3] * 4;
- *new_cb = cb * 8;
- *new_cr = cr * 8;
-
- if (old_y)
- old_y += 2, old_cb++, old_cr++;
- new_y += 2, new_cb++, new_cr++;
- row_index++;
- if (avctx->width / 2 == row_index) {
- row_index = 0;
- if (old_y) {
- old_y += old_y_stride * 2 - avctx->width;
- old_cb += old_cb_stride - avctx->width / 2;
- old_cr += old_cr_stride - avctx->width / 2;
- }
+ *ya++ = y_avg;
+
+ new_y[0] = y[0];
+ new_y[1] = y[1];
+ new_y[new_y_stride] = y[2];
+ new_y[new_y_stride + 1] = y[3];
+ *new_cb = cb;
+ *new_cr = cr;
+
+ old_y += 2;
+ old_cb++;
+ old_cr++;
+ new_y += 2;
+ new_cb++;
+ new_cr++;
+ block_x++;
+ if (block_x * 2 == avctx->width) {
+ block_x = 0;
+ old_y += old_y_stride * 2 - avctx->width;
+ old_cb += old_cb_stride - avctx->width / 2;
+ old_cr += old_cr_stride - avctx->width / 2;
new_y += new_y_stride * 2 - avctx->width;
- new_cb += new_cb_stride - avctx->width / 2;
- new_cr += new_cr_stride - avctx->width / 2;
+ new_cb += new_cb_stride - avctx->width / 2;
+ new_cr += new_cr_stride - avctx->width / 2;
}
skip--;
}
- av_log(avctx, AV_LOG_DEBUG,
- "Escape sizes: %i, %i\n",
- buf_size, get_bits_count(&gb) / 8);
+ new_y = s->new_y;
+ new_cb = s->new_u;
+ new_cr = s->new_v;
+ dstY = pic->data[0];
+ dstU = pic->data[1];
+ dstV = pic->data[2];
+ for (j = 0; j < avctx->height; j++) {
+ for (i = 0; i < avctx->width; i++)
+ dstY[i] = new_y[i] << 2;
+ dstY += pic->linesize[0];
+ new_y += new_y_stride;
+ }
+ for (j = 0; j < avctx->height / 2; j++) {
+ for (i = 0; i < avctx->width / 2; i++) {
+ dstU[i] = chroma_vals[new_cb[i]];
+ dstV[i] = chroma_vals[new_cr[i]];
+ }
+ dstU += pic->linesize[1];
+ dstV += pic->linesize[2];
+ new_cb += new_cb_stride;
+ new_cr += new_cr_stride;
+ }
+
+ av_dlog(avctx, "Frame data: provided %d bytes, used %d bytes\n",
+ buf_size, get_bits_count(&gb) >> 3);
- av_frame_unref(&s->frame);
- if ((ret = av_frame_ref(&s->frame, frame)) < 0)
- return ret;
+ FFSWAP(uint8_t*, s->old_y, s->new_y);
+ FFSWAP(uint8_t*, s->old_u, s->new_u);
+ FFSWAP(uint8_t*, s->old_v, s->new_v);
*got_frame = 1;
return buf_size;
}
-
AVCodec ff_escape130_decoder = {
.name = "escape130",
.type = AVMEDIA_TYPE_VIDEO,