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,
186
p = bytestream2_get_byte(&qctx->buffer);
195
187
for(i = 0; i <= code; i++) {
196
188
dst[filled++] = p;
197
189
if(filled >= width) {
203
197
} else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
206
200
for(i = 0; i <= code; i++) {
207
dst[filled++] = *src++;
201
dst[filled++] = bytestream2_get_byte(&qctx->buffer);
208
202
if(filled >= width) {
215
210
} else if(code >= 0x80) { /* skip code: 0x80..0xBF */
250
246
static int decode_frame(AVCodecContext *avctx,
251
void *data, int *data_size,
247
void *data, int *got_frame,
254
const uint8_t *buf = avpkt->data;
255
int buf_size = avpkt->size;
256
251
QpegContext * const a = avctx->priv_data;
257
AVFrame * const p= (AVFrame*)&a->pic;
252
AVFrame * const p = &a->pic;
258
253
uint8_t* outdata;
260
255
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
257
if (avpkt->size < 0x86) {
258
av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
259
return AVERROR_INVALIDDATA;
262
bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
262
263
p->reference = 3;
263
264
if (avctx->reget_buffer(avctx, p) < 0) {
264
265
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
267
268
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);
269
bytestream2_skip(&a->buffer, 4);
270
bytestream2_get_buffer(&a->buffer, ctable, 128);
271
bytestream2_skip(&a->buffer, 1);
273
delta = bytestream2_get_byte(&a->buffer);
275
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);
277
qpeg_decode_inter(a, outdata, a->pic.linesize[0], avctx->width, avctx->height, delta, ctable, a->refdata);
275
280
/* make the palette available on the way out */
280
285
memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
282
*data_size = sizeof(AVFrame);
283
288
*(AVFrame*)data = a->pic;
288
293
static av_cold int decode_init(AVCodecContext *avctx){
289
294
QpegContext * const a = avctx->priv_data;
291
296
a->avctx = avctx;
292
avctx->pix_fmt= PIX_FMT_PAL8;
297
avctx->pix_fmt= AV_PIX_FMT_PAL8;
293
298
a->refdata = av_malloc(avctx->width * avctx->height);
298
303
static av_cold int decode_end(AVCodecContext *avctx){
299
304
QpegContext * const a = avctx->priv_data;
300
AVFrame * const p= (AVFrame*)&a->pic;
305
AVFrame * const p = &a->pic;
303
308
avctx->release_buffer(avctx, p);
309
314
AVCodec ff_qpeg_decoder = {
311
316
.type = AVMEDIA_TYPE_VIDEO,
317
.id = AV_CODEC_ID_QPEG,
313
318
.priv_data_size = sizeof(QpegContext),
314
319
.init = decode_init,
315
320
.close = decode_end,
316
321
.decode = decode_frame,
317
322
.capabilities = CODEC_CAP_DR1,
318
.long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
323
.long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),