annotate qpeg.c @ 8394:a9132c1bd56a libavcodec

Simplify chroma AC in CABAC residual decoding.
author darkshikari
date Fri, 19 Dec 2008 00:43:30 +0000
parents 322023e630a6
children e9d9d946f213
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
1 /*
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
2 * QPEG codec
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
3 * Copyright (c) 2004 Konstantin Shishkov
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
15 * Lesser General Public License for more details.
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
16 *
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3036
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2979
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
20 */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
21
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
22 /**
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
23 * @file qpeg.c
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
24 * QPEG codec.
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
25 */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
26
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
27 #include "avcodec.h"
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
28
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
29 typedef struct QpegContext{
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
30 AVCodecContext *avctx;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
31 AVFrame pic;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
32 uint8_t *refdata;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
33 } QpegContext;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
34
6274
michael
parents: 5215
diff changeset
35 static void qpeg_decode_intra(const uint8_t *src, uint8_t *dst, int size,
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
36 int stride, int width, int height)
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
37 {
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
38 int i;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
39 int code;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
40 int c0, c1;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
41 int run, copy;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
42 int filled = 0;
2823
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
43 int rows_to_go;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
44
2823
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
45 rows_to_go = height;
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
46 height--;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
47 dst = dst + height * stride;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
48
2823
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
49 while((size > 0) && (rows_to_go > 0)) {
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
50 code = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
51 size--;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
52 run = copy = 0;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
53 if(code == 0xFC) /* end-of-picture code */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
54 break;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
55 if(code >= 0xF8) { /* very long run */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
56 c0 = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
57 c1 = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
58 size -= 2;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
59 run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
60 } else if (code >= 0xF0) { /* long run */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
61 c0 = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
62 size--;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
63 run = ((code & 0xF) << 8) + c0 + 2;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
64 } else if (code >= 0xE0) { /* short run */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
65 run = (code & 0x1F) + 2;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
66 } else if (code >= 0xC0) { /* very long copy */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
67 c0 = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
68 c1 = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
69 size -= 2;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
70 copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
71 } else if (code >= 0x80) { /* long copy */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
72 c0 = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
73 size--;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
74 copy = ((code & 0x7F) << 8) + c0 + 1;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
75 } else { /* short copy */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
76 copy = code + 1;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
77 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
78
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
79 /* perform actual run or copy */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
80 if(run) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
81 int p;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
82
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
83 p = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
84 size--;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
85 for(i = 0; i < run; i++) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
86 dst[filled++] = p;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
87 if (filled >= width) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
88 filled = 0;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
89 dst -= stride;
2823
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
90 rows_to_go--;
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
91 if(rows_to_go <= 0)
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
92 break;
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
93 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
94 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
95 } else {
2823
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
96 size -= copy;
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
97 for(i = 0; i < copy; i++) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
98 dst[filled++] = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
99 if (filled >= width) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
100 filled = 0;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
101 dst -= stride;
2823
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
102 rows_to_go--;
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
103 if(rows_to_go <= 0)
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
104 break;
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
105 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
106 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
107 }
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
108 }
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
109 }
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
110
7129
322023e630a6 mark read-only data as const
stefang
parents: 7040
diff changeset
111 static const int qpeg_table_h[16] =
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
112 { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
7129
322023e630a6 mark read-only data as const
stefang
parents: 7040
diff changeset
113 static const int qpeg_table_w[16] =
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
114 { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
115
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
116 /* Decodes delta frames */
6274
michael
parents: 5215
diff changeset
117 static void qpeg_decode_inter(const uint8_t *src, uint8_t *dst, int size,
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
118 int stride, int width, int height,
6276
e9cff14a5ae5 forgotten const
michael
parents: 6274
diff changeset
119 int delta, const uint8_t *ctable, uint8_t *refdata)
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
120 {
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
121 int i, j;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
122 int code;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
123 int filled = 0;
2823
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
124 int orig_height;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
125
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
126 /* copy prev frame */
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
127 for(i = 0; i < height; i++)
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
128 memcpy(refdata + (i * width), dst + (i * stride), width);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
129
2823
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
130 orig_height = height;
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
131 height--;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
132 dst = dst + height * stride;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
133
2823
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
134 while((size > 0) && (height >= 0)) {
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
135 code = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
136 size--;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
137
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
138 if(delta) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
139 /* motion compensation */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
140 while((code & 0xF0) == 0xF0) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
141 if(delta == 1) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
142 int me_idx;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
143 int me_w, me_h, me_x, me_y;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
144 uint8_t *me_plane;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
145 int corr, val;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
146
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
147 /* get block size by index */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
148 me_idx = code & 0xF;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
149 me_w = qpeg_table_w[me_idx];
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
150 me_h = qpeg_table_h[me_idx];
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
151
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
152 /* extract motion vector */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
153 corr = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
154 size--;
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
155
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
156 val = corr >> 4;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
157 if(val > 7)
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
158 val -= 16;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
159 me_x = val;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
160
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
161 val = corr & 0xF;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
162 if(val > 7)
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
163 val -= 16;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
164 me_y = val;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
165
2823
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
166 /* check motion vector */
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
167 if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
168 (height - me_y - me_h < 0) || (height - me_y > orig_height) ||
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
169 (filled + me_w > width) || (height - me_h < 0))
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
170 av_log(NULL, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
171 me_x, me_y, me_w, me_h, filled, height);
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
172 else {
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
173 /* do motion compensation */
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
174 me_plane = refdata + (filled + me_x) + (height - me_y) * width;
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
175 for(j = 0; j < me_h; j++) {
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
176 for(i = 0; i < me_w; i++)
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
177 dst[filled + i - (j * stride)] = me_plane[i - (j * width)];
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
178 }
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
179 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
180 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
181 code = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
182 size--;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
183 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
184 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
185
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
186 if(code == 0xE0) /* end-of-picture code */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
187 break;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
188 if(code > 0xE0) { /* run code: 0xE1..0xFF */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
189 int p;
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
190
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
191 code &= 0x1F;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
192 p = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
193 size--;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
194 for(i = 0; i <= code; i++) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
195 dst[filled++] = p;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
196 if(filled >= width) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
197 filled = 0;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
198 dst -= stride;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
199 height--;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
200 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
201 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
202 } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
203 code &= 0x1F;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
204
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
205 for(i = 0; i <= code; i++) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
206 dst[filled++] = *src++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
207 if(filled >= width) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
208 filled = 0;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
209 dst -= stride;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
210 height--;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
211 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
212 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
213 size -= code + 1;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
214 } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
215 int skip;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
216
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
217 code &= 0x3F;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
218 /* codes 0x80 and 0x81 are actually escape codes,
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
219 skip value minus constant is in the next byte */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
220 if(!code)
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
221 skip = (*src++) + 64;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
222 else if(code == 1)
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
223 skip = (*src++) + 320;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
224 else
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
225 skip = code;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
226 filled += skip;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
227 while( filled >= width) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
228 filled -= width;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
229 dst -= stride;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
230 height--;
2823
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
231 if(height < 0)
6bf98adb22a1 security fixes
michael
parents: 2586
diff changeset
232 break;
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
233 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
234 } else {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
235 /* zero code treated as one-pixel skip */
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
236 if(code)
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
237 dst[filled++] = ctable[code & 0x7F];
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
238 else
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
239 filled++;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
240 if(filled >= width) {
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
241 filled = 0;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
242 dst -= stride;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
243 height--;
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
244 }
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
245 }
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
246 }
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
247 }
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
248
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
249 static int decode_frame(AVCodecContext *avctx,
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
250 void *data, int *data_size,
6274
michael
parents: 5215
diff changeset
251 const uint8_t *buf, int buf_size)
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
252 {
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
253 QpegContext * const a = avctx->priv_data;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
254 AVFrame * const p= (AVFrame*)&a->pic;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
255 uint8_t* outdata;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
256 int delta;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
257
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
258 if(p->data[0])
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
259 avctx->release_buffer(avctx, p);
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
260
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
261 p->reference= 0;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
262 if(avctx->get_buffer(avctx, p) < 0){
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
263 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
264 return -1;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
265 }
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
266 outdata = a->pic.data[0];
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
267 if(buf[0x85] == 0x10) {
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
268 qpeg_decode_intra(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height);
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
269 } else {
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
270 delta = buf[0x85];
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
271 qpeg_decode_inter(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height, delta, buf + 4, a->refdata);
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
272 }
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
273
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
274 /* make the palette available on the way out */
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
275 memcpy(a->pic.data[1], a->avctx->palctrl->palette, AVPALETTE_SIZE);
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
276 if (a->avctx->palctrl->palette_changed) {
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
277 a->pic.palette_has_changed = 1;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
278 a->avctx->palctrl->palette_changed = 0;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
279 }
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
280
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
281 *data_size = sizeof(AVFrame);
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
282 *(AVFrame*)data = a->pic;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
283
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
284 return buf_size;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
285 }
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
286
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6450
diff changeset
287 static av_cold int decode_init(AVCodecContext *avctx){
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
288 QpegContext * const a = avctx->priv_data;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
289
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
290 a->avctx = avctx;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
291 avctx->pix_fmt= PIX_FMT_PAL8;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
292 a->pic.data[0] = NULL;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
293 a->refdata = av_malloc(avctx->width * avctx->height);
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
294
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
295 return 0;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
296 }
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
297
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6450
diff changeset
298 static av_cold int decode_end(AVCodecContext *avctx){
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
299 QpegContext * const a = avctx->priv_data;
2586
00d9abc5f76f free that last frame
melanson
parents: 2453
diff changeset
300 AVFrame * const p= (AVFrame*)&a->pic;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2823
diff changeset
301
2586
00d9abc5f76f free that last frame
melanson
parents: 2453
diff changeset
302 if(p->data[0])
00d9abc5f76f free that last frame
melanson
parents: 2453
diff changeset
303 avctx->release_buffer(avctx, p);
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
304
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
305 av_free(a->refdata);
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
306 return 0;
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
307 }
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
308
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
309 AVCodec qpeg_decoder = {
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
310 "qpeg",
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
311 CODEC_TYPE_VIDEO,
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
312 CODEC_ID_QPEG,
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
313 sizeof(QpegContext),
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
314 decode_init,
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
315 NULL,
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
316 decode_end,
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
317 decode_frame,
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
318 CODEC_CAP_DR1,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6710
diff changeset
319 .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
2355
69fcdad5f7d5 native QPEG video decoder, courtesy of Konstantin Shishkov
melanson
parents:
diff changeset
320 };