FFmpeg
Loading...
Searching...
No Matches
qpeg.c
Go to the documentation of this file.
1/*
2 * QPEG codec
3 * Copyright (c) 2004 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * QPEG codec.
25 */
26
27#include "avcodec.h"
28#include "bytestream.h"
29#include "codec_internal.h"
30#include "decode.h"
31
33
40
41static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
42 int stride, int width, int height)
43{
44 int i;
45 int code;
46 int c0, c1;
47 int run, copy;
48 int filled = 0;
49 int rows_to_go;
50
51 rows_to_go = height;
52 height--;
53 dst = dst + height * stride;
54
55 while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
56 code = bytestream2_get_byte(&qctx->buffer);
57 run = copy = 0;
58 if(code == 0xFC) /* end-of-picture code */
59 break;
60 if(code >= 0xF8) { /* very long run */
61 c0 = bytestream2_get_byte(&qctx->buffer);
62 c1 = bytestream2_get_byte(&qctx->buffer);
63 run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
64 } else if (code >= 0xF0) { /* long run */
65 c0 = bytestream2_get_byte(&qctx->buffer);
66 run = ((code & 0xF) << 8) + c0 + 2;
67 } else if (code >= 0xE0) { /* short run */
68 run = (code & 0x1F) + 2;
69 } else if (code >= 0xC0) { /* very long copy */
70 c0 = bytestream2_get_byte(&qctx->buffer);
71 c1 = bytestream2_get_byte(&qctx->buffer);
72 copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
73 } else if (code >= 0x80) { /* long copy */
74 c0 = bytestream2_get_byte(&qctx->buffer);
75 copy = ((code & 0x7F) << 8) + c0 + 1;
76 } else { /* short copy */
77 copy = code + 1;
78 }
79
80 /* perform actual run or copy */
81 if(run) {
82 int p;
83
84 p = bytestream2_get_byte(&qctx->buffer);
85 for(i = 0; i < run; i++) {
86 int step = FFMIN(run - i, width - filled);
87 memset(dst+filled, p, step);
88 filled += step;
89 i += step - 1;
90 if (filled >= width) {
91 filled = 0;
92 dst -= stride;
93 rows_to_go--;
94 while (run - i > width && rows_to_go > 0) {
95 memset(dst, p, width);
96 dst -= stride;
97 rows_to_go--;
98 i += width;
99 }
100 if(rows_to_go <= 0)
101 break;
102 }
103 }
104 } else {
107 while (copy > 0) {
108 int step = FFMIN(copy, width - filled);
109 bytestream2_get_bufferu(&qctx->buffer, dst + filled, step);
110 filled += step;
111 copy -= step;
112 if (filled >= width) {
113 filled = 0;
114 dst -= stride;
115 rows_to_go--;
116 if(rows_to_go <= 0)
117 break;
118 }
119 }
120 }
121 }
122}
123
124static const uint8_t qpeg_table_h[16] =
125 { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
126static const uint8_t qpeg_table_w[16] =
127 { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
128
129/* Decodes delta frames */
130static void av_noinline qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
131 int stride, int width, int height,
132 int delta, const uint8_t *ctable,
133 uint8_t *refdata)
134{
135 int i, j;
136 int code;
137 int filled = 0;
138 int orig_height;
139
140 if (refdata) {
141 /* copy prev frame */
142 for (i = 0; i < height; i++)
143 memcpy(dst + (i * stride), refdata + (i * stride), width);
144 } else {
145 refdata = dst;
146 }
147
148 orig_height = height;
149 height--;
150 dst = dst + height * stride;
151
152 while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
153 code = bytestream2_get_byte(&qctx->buffer);
154
155 if(delta) {
156 /* motion compensation */
157 while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
158 if(delta == 1) {
159 int me_idx;
160 int me_w, me_h, me_x, me_y;
161 uint8_t *me_plane;
162 int corr, val;
163
164 /* get block size by index */
165 me_idx = code & 0xF;
166 me_w = qpeg_table_w[me_idx];
167 me_h = qpeg_table_h[me_idx];
168
169 /* extract motion vector */
170 corr = bytestream2_get_byte(&qctx->buffer);
171
172 val = corr >> 4;
173 if(val > 7)
174 val -= 16;
175 me_x = val;
176
177 val = corr & 0xF;
178 if(val > 7)
179 val -= 16;
180 me_y = val;
181
182 /* check motion vector */
183 if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
184 (height - me_y - me_h < 0) || (height - me_y >= orig_height) ||
185 (filled + me_w > width) || (height - me_h < 0))
186 av_log(qctx->avctx, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
187 me_x, me_y, me_w, me_h, filled, height);
188 else {
189 /* do motion compensation */
190 me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
191 for(j = 0; j < me_h; j++) {
192 for(i = 0; i < me_w; i++)
193 dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
194 }
195 }
196 }
197 code = bytestream2_get_byte(&qctx->buffer);
198 }
199 }
200
201 if(code == 0xE0) /* end-of-picture code */
202 break;
203 if(code > 0xE0) { /* run code: 0xE1..0xFF */
204 int p;
205
206 code &= 0x1F;
207 p = bytestream2_get_byte(&qctx->buffer);
208 for(i = 0; i <= code; i++) {
209 dst[filled++] = p;
210 if(filled >= width) {
211 filled = 0;
212 dst -= stride;
213 height--;
214 if (height < 0)
215 break;
216 }
217 }
218 } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
219 code &= 0x1F;
220
221 if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
222 break;
223
224 for(i = 0; i <= code; i++) {
225 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
226 if(filled >= width) {
227 filled = 0;
228 dst -= stride;
229 height--;
230 if (height < 0)
231 break;
232 }
233 }
234 } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
235 int skip;
236
237 code &= 0x3F;
238 /* codes 0x80 and 0x81 are actually escape codes,
239 skip value minus constant is in the next byte */
240 if(!code)
241 skip = bytestream2_get_byte(&qctx->buffer) + 64;
242 else if(code == 1)
243 skip = bytestream2_get_byte(&qctx->buffer) + 320;
244 else
245 skip = code;
246 filled += skip;
247 while( filled >= width) {
248 filled -= width;
249 dst -= stride;
250 height--;
251 if(height < 0)
252 break;
253 }
254 } else {
255 /* zero code treated as one-pixel skip */
256 if(code) {
257 dst[filled++] = ctable[code & 0x7F];
258 }
259 else
260 filled++;
261 if(filled >= width) {
262 filled = 0;
263 dst -= stride;
264 height--;
265 }
266 }
267 }
268}
269
270static int decode_frame(AVCodecContext *avctx, AVFrame *p,
271 int *got_frame, AVPacket *avpkt)
272{
273 uint8_t ctable[128];
274 QpegContext * const a = avctx->priv_data;
275 AVFrame * const ref = a->ref;
276 uint8_t* outdata;
277 int delta, intra, ret;
278
279 if (avpkt->size < 0x86) {
280 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
281 return AVERROR_INVALIDDATA;
282 }
283
284 bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
285
286 if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0)
287 return ret;
288 outdata = p->data[0];
289 bytestream2_skip(&a->buffer, 4);
290 bytestream2_get_buffer(&a->buffer, ctable, 128);
291 bytestream2_skip(&a->buffer, 1);
292
293 delta = bytestream2_get_byte(&a->buffer);
294 intra = delta == 0x10;
295 if (intra) {
296 qpeg_decode_intra(a, outdata, p->linesize[0], avctx->width, avctx->height);
297 } else {
298 qpeg_decode_inter(a, outdata, p->linesize[0], avctx->width, avctx->height, delta, ctable, ref->data[0]);
299 }
300
301 /* make the palette available on the way out */
302 ff_copy_palette(a->pal, avpkt, avctx);
303 memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
304
305 if ((ret = av_frame_replace(ref, p)) < 0)
306 return ret;
307
308 if (intra)
309 p->flags |= AV_FRAME_FLAG_KEY;
310 else
311 p->flags &= ~AV_FRAME_FLAG_KEY;
312 p->pict_type = intra ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
313
314 *got_frame = 1;
315
316 return avpkt->size;
317}
318
320{
321 QpegContext * const a = avctx->priv_data;
322 int i, pal_size;
323 const uint8_t *pal_src;
324
325 av_frame_unref(a->ref);
326
327 pal_size = FFMIN(1024U, avctx->extradata_size);
328 pal_src = avctx->extradata + avctx->extradata_size - pal_size;
329
330 for (i=0; i<pal_size/4; i++)
331 a->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
332}
333
335{
336 QpegContext * const a = avctx->priv_data;
337
338 av_frame_free(&a->ref);
339
340 return 0;
341}
342
344 QpegContext * const a = avctx->priv_data;
345
346 a->avctx = avctx;
347 avctx->pix_fmt= AV_PIX_FMT_PAL8;
348
349 a->ref = av_frame_alloc();
350 if (!a->ref)
351 return AVERROR(ENOMEM);
352
353 decode_flush(avctx);
354
355 return 0;
356}
357
359 .p.name = "qpeg",
360 CODEC_LONG_NAME("Q-team QPEG"),
361 .p.type = AVMEDIA_TYPE_VIDEO,
362 .p.id = AV_CODEC_ID_QPEG,
363 .priv_data_size = sizeof(QpegContext),
364 .init = decode_init,
365 .close = decode_end,
367 .flush = decode_flush,
368 .p.capabilities = AV_CODEC_CAP_DR1,
369 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
370};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static double val(void *priv, double ch)
Definition aeval.c:77
static av_cold void decode_flush(AVCodecContext *avctx)
Definition agm.c:1253
const FFCodec ff_qpeg_decoder
Definition qpeg.c:358
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:267
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:277
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
Check whether the side-data of src contains a palette of size AVPALETTE_SIZE; if so,...
Definition decode.c:2321
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition avcodec.h:415
@ AV_CODEC_ID_QPEG
Definition codec_id.h:110
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition frame.c:376
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
int a
#define AV_RL32(p)
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static av_cold int decode_end(AVCodecContext *avctx)
Definition 4xm.c:980
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
Macro definitions for various function/variable attributes.
#define av_noinline
Definition attributes.h:97
#define av_cold
Definition attributes.h:117
#define FFMIN(a, b)
Definition macros.h:49
static const uint64_t c1
Definition murmur3.c:52
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
#define AVPALETTE_SIZE
Definition pixfmt.h:32
static av_cold void decode_flush(AVCodecContext *avctx)
Definition qpeg.c:319
static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst, int stride, int width, int height)
Definition qpeg.c:41
static av_cold int decode_init(AVCodecContext *avctx)
Definition qpeg.c:343
static const uint8_t qpeg_table_h[16]
Definition qpeg.c:124
static av_cold int decode_end(AVCodecContext *avctx)
Definition qpeg.c:334
static void av_noinline qpeg_decode_inter(QpegContext *qctx, uint8_t *dst, int stride, int width, int height, int delta, const uint8_t *ctable, uint8_t *refdata)
Definition qpeg.c:130
static int decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition qpeg.c:270
static const uint8_t qpeg_table_w[16]
Definition qpeg.c:126
const uint8_t * code
Definition spdifenc.c:433
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
GetByteContext buffer
Definition qpeg.c:38
uint32_t pal[256]
Definition qpeg.c:37
AVCodecContext * avctx
Definition qpeg.c:35
AVFrame * ref
Definition qpeg.c:36
uint8_t run
Definition svq3.c:207
#define stride
#define av_log(a,...)
static int ref[MAX_W *MAX_W]
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
static void copy(const float *p1, float *p2, const int length)
float delta