27
27
#include "avcodec.h"
28
#include "bytestream.h"
29
30
typedef struct QpegContext{
30
31
AVCodecContext *avctx;
35
GetByteContext buffer;
36
static void qpeg_decode_intra(const uint8_t *src, uint8_t *dst, int size,
37
int stride, int width, int height)
38
static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
39
int stride, int width, int height)
48
50
dst = dst + height * stride;
50
while((size > 0) && (rows_to_go > 0)) {
52
while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
53
code = bytestream2_get_byte(&qctx->buffer);
54
55
if(code == 0xFC) /* end-of-picture code */
56
57
if(code >= 0xF8) { /* very long run */
58
c0 = bytestream2_get_byte(&qctx->buffer);
59
c1 = bytestream2_get_byte(&qctx->buffer);
60
60
run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
61
61
} else if (code >= 0xF0) { /* long run */
62
c0 = bytestream2_get_byte(&qctx->buffer);
64
63
run = ((code & 0xF) << 8) + c0 + 2;
65
64
} else if (code >= 0xE0) { /* short run */
66
65
run = (code & 0x1F) + 2;
67
66
} else if (code >= 0xC0) { /* very long copy */
67
c0 = bytestream2_get_byte(&qctx->buffer);
68
c1 = bytestream2_get_byte(&qctx->buffer);
71
69
copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
72
70
} else if (code >= 0x80) { /* long copy */
71
c0 = bytestream2_get_byte(&qctx->buffer);
75
72
copy = ((code & 0x7F) << 8) + c0 + 1;
76
73
} else { /* short copy */
115
110
{ 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
117
112
/* Decodes delta frames */
118
static void qpeg_decode_inter(const uint8_t *src, uint8_t *dst, int size,
119
int stride, int width, int height,
120
int delta, const uint8_t *ctable, uint8_t *refdata)
113
static void qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
114
int stride, int width, int height,
115
int delta, const uint8_t *ctable,
250
242
static int decode_frame(AVCodecContext *avctx,
251
void *data, int *data_size,
243
void *data, int *got_frame,
254
const uint8_t *buf = avpkt->data;
255
int buf_size = avpkt->size;
256
247
QpegContext * const a = avctx->priv_data;
257
AVFrame * const p= (AVFrame*)&a->pic;
248
AVFrame * const p = &a->pic;
258
249
uint8_t* outdata;
260
251
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
253
if (avpkt->size < 0x86) {
254
av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
255
return AVERROR_INVALIDDATA;
258
bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
262
259
p->reference = 3;
263
260
if (avctx->reget_buffer(avctx, p) < 0) {
264
261
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
267
264
outdata = a->pic.data[0];
268
if(buf[0x85] == 0x10) {
269
qpeg_decode_intra(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height);
265
bytestream2_skip(&a->buffer, 4);
266
bytestream2_get_buffer(&a->buffer, ctable, 128);
267
bytestream2_skip(&a->buffer, 1);
269
delta = bytestream2_get_byte(&a->buffer);
271
qpeg_decode_intra(a, outdata, a->pic.linesize[0], avctx->width, avctx->height);
272
qpeg_decode_inter(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height, delta, buf + 4, a->refdata);
273
qpeg_decode_inter(a, outdata, a->pic.linesize[0], avctx->width, avctx->height, delta, ctable, a->refdata);
275
276
/* make the palette available on the way out */
280
281
memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
282
*data_size = sizeof(AVFrame);
283
284
*(AVFrame*)data = a->pic;
288
289
static av_cold int decode_init(AVCodecContext *avctx){
289
290
QpegContext * const a = avctx->priv_data;
291
292
a->avctx = avctx;
292
avctx->pix_fmt= PIX_FMT_PAL8;
293
avctx->pix_fmt= AV_PIX_FMT_PAL8;
293
294
a->refdata = av_malloc(avctx->width * avctx->height);
298
299
static av_cold int decode_end(AVCodecContext *avctx){
299
300
QpegContext * const a = avctx->priv_data;
300
AVFrame * const p= (AVFrame*)&a->pic;
301
AVFrame * const p = &a->pic;
303
304
avctx->release_buffer(avctx, p);
309
310
AVCodec ff_qpeg_decoder = {
311
312
.type = AVMEDIA_TYPE_VIDEO,
313
.id = AV_CODEC_ID_QPEG,
313
314
.priv_data_size = sizeof(QpegContext),
314
315
.init = decode_init,
315
316
.close = decode_end,
316
317
.decode = decode_frame,
317
318
.capabilities = CODEC_CAP_DR1,
318
.long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
319
.long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),