Mercurial > libavcodec.hg
annotate mjpeg.c @ 384:d442918c4698 libavcodec
print frame decoding time support (x86 only)
author | michaelni |
---|---|
date | Thu, 16 May 2002 22:01:30 +0000 |
parents | 749b5c16c0f7 |
children | fce0a2520551 |
rev | line source |
---|---|
0 | 1 /* |
23 | 2 * MJPEG encoder and decoder |
3 * Copyright (c) 2000, 2001 Gerard Lantau. | |
0 | 4 * |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
18 * |
354 | 19 * Support for external huffman table and various fixes (AVID workaround) by |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
20 * Alex Beregszaszi <alex@naxine.org> |
0 | 21 */ |
76 | 22 //#define DEBUG |
0 | 23 #include "avcodec.h" |
24 #include "dsputil.h" | |
25 #include "mpegvideo.h" | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
26 #include "common.h" |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
27 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
28 #include <string.h> |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
29 #include <stdio.h> |
0 | 30 |
354 | 31 #ifdef USE_FASTMEMCPY |
32 #include "fastmemcpy.h" | |
33 #endif | |
34 | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
35 /* use two quantizer table (one for luminance and one for chrominance) */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
36 /* not yet working */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
37 #undef TWOMATRIXES |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
38 |
0 | 39 typedef struct MJpegContext { |
40 UINT8 huff_size_dc_luminance[12]; | |
41 UINT16 huff_code_dc_luminance[12]; | |
42 UINT8 huff_size_dc_chrominance[12]; | |
43 UINT16 huff_code_dc_chrominance[12]; | |
44 | |
45 UINT8 huff_size_ac_luminance[256]; | |
46 UINT16 huff_code_ac_luminance[256]; | |
47 UINT8 huff_size_ac_chrominance[256]; | |
48 UINT16 huff_code_ac_chrominance[256]; | |
49 } MJpegContext; | |
50 | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
51 /* JPEG marker codes */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
52 typedef enum { |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
53 /* start of frame */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
54 SOF0 = 0xc0, /* baseline */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
55 SOF1 = 0xc1, /* extended sequential, huffman */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
56 SOF2 = 0xc2, /* progressive, huffman */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
57 SOF3 = 0xc3, /* lossless, huffman */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
58 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
59 SOF5 = 0xc5, /* differential sequential, huffman */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
60 SOF6 = 0xc6, /* differential progressive, huffman */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
61 SOF7 = 0xc7, /* differential lossless, huffman */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
62 JPG = 0xc8, /* reserved for JPEG extension */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
63 SOF9 = 0xc9, /* extended sequential, arithmetic */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
64 SOF10 = 0xca, /* progressive, arithmetic */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
65 SOF11 = 0xcb, /* lossless, arithmetic */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
66 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
67 SOF13 = 0xcd, /* differential sequential, arithmetic */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
68 SOF14 = 0xce, /* differential progressive, arithmetic */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
69 SOF15 = 0xcf, /* differential lossless, arithmetic */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
70 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
71 DHT = 0xc4, /* define huffman tables */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
72 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
73 DAC = 0xcc, /* define arithmetic-coding conditioning */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
74 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
75 /* restart with modulo 8 count "m" */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
76 RST0 = 0xd0, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
77 RST1 = 0xd1, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
78 RST2 = 0xd2, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
79 RST3 = 0xd3, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
80 RST4 = 0xd4, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
81 RST5 = 0xd5, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
82 RST6 = 0xd6, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
83 RST7 = 0xd7, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
84 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
85 SOI = 0xd8, /* start of image */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
86 EOI = 0xd9, /* end of image */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
87 SOS = 0xda, /* start of scan */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
88 DQT = 0xdb, /* define quantization tables */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
89 DNL = 0xdc, /* define number of lines */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
90 DRI = 0xdd, /* define restart interval */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
91 DHP = 0xde, /* define hierarchical progression */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
92 EXP = 0xdf, /* expand reference components */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
93 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
94 APP0 = 0xe0, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
95 APP1 = 0xe1, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
96 APP2 = 0xe2, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
97 APP3 = 0xe3, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
98 APP4 = 0xe4, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
99 APP5 = 0xe5, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
100 APP6 = 0xe6, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
101 APP7 = 0xe7, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
102 APP8 = 0xe8, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
103 APP9 = 0xe9, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
104 APP10 = 0xea, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
105 APP11 = 0xeb, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
106 APP12 = 0xec, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
107 APP13 = 0xed, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
108 APP14 = 0xee, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
109 APP15 = 0xef, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
110 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
111 JPG0 = 0xf0, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
112 JPG1 = 0xf1, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
113 JPG2 = 0xf2, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
114 JPG3 = 0xf3, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
115 JPG4 = 0xf4, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
116 JPG5 = 0xf5, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
117 JPG6 = 0xf6, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
118 JPG7 = 0xf7, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
119 JPG8 = 0xf8, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
120 JPG9 = 0xf9, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
121 JPG10 = 0xfa, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
122 JPG11 = 0xfb, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
123 JPG12 = 0xfc, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
124 JPG13 = 0xfd, |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
125 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
126 COM = 0xfe, /* comment */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
127 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
128 TEM = 0x01, /* temporary private use for arithmetic coding */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
129 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
130 /* 0x02 -> 0xbf reserved */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
131 } JPEG_MARKER; |
0 | 132 |
133 #if 0 | |
134 /* These are the sample quantization tables given in JPEG spec section K.1. | |
135 * The spec says that the values given produce "good" quality, and | |
136 * when divided by 2, "very good" quality. | |
137 */ | |
138 static const unsigned char std_luminance_quant_tbl[64] = { | |
139 16, 11, 10, 16, 24, 40, 51, 61, | |
140 12, 12, 14, 19, 26, 58, 60, 55, | |
141 14, 13, 16, 24, 40, 57, 69, 56, | |
142 14, 17, 22, 29, 51, 87, 80, 62, | |
143 18, 22, 37, 56, 68, 109, 103, 77, | |
144 24, 35, 55, 64, 81, 104, 113, 92, | |
145 49, 64, 78, 87, 103, 121, 120, 101, | |
146 72, 92, 95, 98, 112, 100, 103, 99 | |
147 }; | |
148 static const unsigned char std_chrominance_quant_tbl[64] = { | |
149 17, 18, 24, 47, 99, 99, 99, 99, | |
150 18, 21, 26, 66, 99, 99, 99, 99, | |
151 24, 26, 56, 99, 99, 99, 99, 99, | |
152 47, 66, 99, 99, 99, 99, 99, 99, | |
153 99, 99, 99, 99, 99, 99, 99, 99, | |
154 99, 99, 99, 99, 99, 99, 99, 99, | |
155 99, 99, 99, 99, 99, 99, 99, 99, | |
156 99, 99, 99, 99, 99, 99, 99, 99 | |
157 }; | |
158 #endif | |
159 | |
160 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */ | |
161 /* IMPORTANT: these are only valid for 8-bit data precision! */ | |
162 static const UINT8 bits_dc_luminance[17] = | |
163 { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }; | |
164 static const UINT8 val_dc_luminance[] = | |
165 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; | |
166 | |
167 static const UINT8 bits_dc_chrominance[17] = | |
168 { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; | |
169 static const UINT8 val_dc_chrominance[] = | |
170 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; | |
171 | |
172 static const UINT8 bits_ac_luminance[17] = | |
173 { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }; | |
174 static const UINT8 val_ac_luminance[] = | |
175 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, | |
176 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, | |
177 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, | |
178 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, | |
179 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, | |
180 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, | |
181 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, | |
182 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, | |
183 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, | |
184 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, | |
185 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, | |
186 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, | |
187 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, | |
188 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, | |
189 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, | |
190 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, | |
191 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, | |
192 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, | |
193 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, | |
194 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, | |
195 0xf9, 0xfa | |
196 }; | |
197 | |
198 static const UINT8 bits_ac_chrominance[17] = | |
199 { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }; | |
200 | |
201 static const UINT8 val_ac_chrominance[] = | |
202 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, | |
203 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, | |
204 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, | |
205 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, | |
206 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, | |
207 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, | |
208 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, | |
209 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, | |
210 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, | |
211 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, | |
212 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, | |
213 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, | |
214 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, | |
215 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, | |
216 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, | |
217 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, | |
218 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, | |
219 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, | |
220 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, | |
221 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, | |
222 0xf9, 0xfa | |
223 }; | |
224 | |
225 /* isn't this function nicer than the one in the libjpeg ? */ | |
226 static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code, | |
227 const UINT8 *bits_table, const UINT8 *val_table) | |
228 { | |
229 int i, j, k,nb, code, sym; | |
230 | |
231 code = 0; | |
232 k = 0; | |
233 for(i=1;i<=16;i++) { | |
234 nb = bits_table[i]; | |
235 for(j=0;j<nb;j++) { | |
236 sym = val_table[k++]; | |
237 huff_size[sym] = i; | |
238 huff_code[sym] = code; | |
239 code++; | |
240 } | |
241 code <<= 1; | |
242 } | |
243 } | |
244 | |
245 int mjpeg_init(MpegEncContext *s) | |
246 { | |
247 MJpegContext *m; | |
248 | |
249 m = malloc(sizeof(MJpegContext)); | |
250 if (!m) | |
251 return -1; | |
344 | 252 |
253 s->min_qcoeff=-1023; | |
254 s->max_qcoeff= 1023; | |
357 | 255 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x |
0 | 256 |
257 /* build all the huffman tables */ | |
258 build_huffman_codes(m->huff_size_dc_luminance, | |
259 m->huff_code_dc_luminance, | |
260 bits_dc_luminance, | |
261 val_dc_luminance); | |
262 build_huffman_codes(m->huff_size_dc_chrominance, | |
263 m->huff_code_dc_chrominance, | |
264 bits_dc_chrominance, | |
265 val_dc_chrominance); | |
266 build_huffman_codes(m->huff_size_ac_luminance, | |
267 m->huff_code_ac_luminance, | |
268 bits_ac_luminance, | |
269 val_ac_luminance); | |
270 build_huffman_codes(m->huff_size_ac_chrominance, | |
271 m->huff_code_ac_chrominance, | |
272 bits_ac_chrominance, | |
273 val_ac_chrominance); | |
274 | |
275 s->mjpeg_ctx = m; | |
276 return 0; | |
277 } | |
278 | |
279 void mjpeg_close(MpegEncContext *s) | |
280 { | |
281 free(s->mjpeg_ctx); | |
282 } | |
283 | |
284 static inline void put_marker(PutBitContext *p, int code) | |
285 { | |
286 put_bits(p, 8, 0xff); | |
287 put_bits(p, 8, code); | |
288 } | |
289 | |
290 /* table_class: 0 = DC coef, 1 = AC coefs */ | |
291 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id, | |
292 const UINT8 *bits_table, const UINT8 *value_table) | |
293 { | |
294 PutBitContext *p = &s->pb; | |
295 int n, i; | |
296 | |
297 put_bits(p, 4, table_class); | |
298 put_bits(p, 4, table_id); | |
299 | |
300 n = 0; | |
301 for(i=1;i<=16;i++) { | |
302 n += bits_table[i]; | |
303 put_bits(p, 8, bits_table[i]); | |
304 } | |
305 | |
306 for(i=0;i<n;i++) | |
307 put_bits(p, 8, value_table[i]); | |
308 | |
309 return n + 17; | |
310 } | |
311 | |
312 static void jpeg_table_header(MpegEncContext *s) | |
313 { | |
314 PutBitContext *p = &s->pb; | |
37 | 315 int i, j, size; |
0 | 316 UINT8 *ptr; |
317 | |
318 /* quant matrixes */ | |
319 put_marker(p, DQT); | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
320 #ifdef TWOMATRIXES |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
321 put_bits(p, 16, 2 + 2 * (1 + 64)); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
322 #else |
0 | 323 put_bits(p, 16, 2 + 1 * (1 + 64)); |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
324 #endif |
0 | 325 put_bits(p, 4, 0); /* 8 bit precision */ |
326 put_bits(p, 4, 0); /* table 0 */ | |
327 for(i=0;i<64;i++) { | |
37 | 328 j = zigzag_direct[i]; |
329 put_bits(p, 8, s->intra_matrix[j]); | |
0 | 330 } |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
331 #ifdef TWOMATRIXES |
0 | 332 put_bits(p, 4, 0); /* 8 bit precision */ |
333 put_bits(p, 4, 1); /* table 1 */ | |
334 for(i=0;i<64;i++) { | |
37 | 335 j = zigzag_direct[i]; |
336 put_bits(p, 8, s->chroma_intra_matrix[j]); | |
0 | 337 } |
338 #endif | |
339 | |
340 /* huffman table */ | |
341 put_marker(p, DHT); | |
342 flush_put_bits(p); | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
229
diff
changeset
|
343 ptr = pbBufPtr(p); |
0 | 344 put_bits(p, 16, 0); /* patched later */ |
345 size = 2; | |
346 size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance); | |
347 size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance); | |
348 | |
349 size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance); | |
350 size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance); | |
351 ptr[0] = size >> 8; | |
352 ptr[1] = size; | |
353 } | |
354 | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
355 static void jpeg_put_comments(MpegEncContext *s) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
356 { |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
357 PutBitContext *p = &s->pb; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
358 int size; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
359 UINT8 *ptr; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
360 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
361 #if 0 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
362 /* JFIF header */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
363 put_marker(p, APP0); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
364 put_bits(p, 16, 16); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
365 put_string(p, "JFIF"); /* this puts the trailing zero-byte too */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
366 put_bits(p, 16, 0x101); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
367 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
368 put_bits(p, 16, 1); /* aspect: 1:1 */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
369 put_bits(p, 16, 1); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
370 put_bits(p, 8, 0); /* thumbnail width */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
371 put_bits(p, 8, 0); /* thumbnail height */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
372 #endif |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
373 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
374 /* comment */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
375 put_marker(p, COM); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
376 flush_put_bits(p); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
377 ptr = pbBufPtr(p); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
378 put_bits(p, 16, 0); /* patched later */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
379 #define VERSION "FFmpeg" LIBAVCODEC_VERSION "b" LIBAVCODEC_BUILD_STR |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
380 put_string(p, VERSION); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
381 size = strlen(VERSION)+3; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
382 #undef VERSION |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
383 ptr[0] = size >> 8; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
384 ptr[1] = size; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
385 } |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
386 |
0 | 387 void mjpeg_picture_header(MpegEncContext *s) |
388 { | |
389 put_marker(&s->pb, SOI); | |
390 | |
370
0eca28d16cbd
clamp intra matrix to 8bit for mjpeg (workaround for qscale>=25)
al3x
parents:
369
diff
changeset
|
391 if (!s->mjpeg_data_only_frames) |
0eca28d16cbd
clamp intra matrix to 8bit for mjpeg (workaround for qscale>=25)
al3x
parents:
369
diff
changeset
|
392 { |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
393 jpeg_put_comments(s); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
394 |
229 | 395 if (s->mjpeg_write_tables) jpeg_table_header(s); |
0 | 396 |
397 put_marker(&s->pb, SOF0); | |
398 | |
399 put_bits(&s->pb, 16, 17); | |
400 put_bits(&s->pb, 8, 8); /* 8 bits/component */ | |
401 put_bits(&s->pb, 16, s->height); | |
402 put_bits(&s->pb, 16, s->width); | |
403 put_bits(&s->pb, 8, 3); /* 3 components */ | |
404 | |
405 /* Y component */ | |
406 put_bits(&s->pb, 8, 1); /* component number */ | |
229 | 407 put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */ |
408 put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */ | |
0 | 409 put_bits(&s->pb, 8, 0); /* select matrix */ |
410 | |
411 /* Cb component */ | |
412 put_bits(&s->pb, 8, 2); /* component number */ | |
229 | 413 put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */ |
414 put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */ | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
415 #ifdef TWOMATRIXES |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
416 put_bits(&s->pb, 8, 1); /* select matrix */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
417 #else |
0 | 418 put_bits(&s->pb, 8, 0); /* select matrix */ |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
419 #endif |
0 | 420 |
421 /* Cr component */ | |
422 put_bits(&s->pb, 8, 3); /* component number */ | |
229 | 423 put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */ |
424 put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */ | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
425 #ifdef TWOMATRIXES |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
426 put_bits(&s->pb, 8, 1); /* select matrix */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
427 #else |
0 | 428 put_bits(&s->pb, 8, 0); /* select matrix */ |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
429 #endif |
370
0eca28d16cbd
clamp intra matrix to 8bit for mjpeg (workaround for qscale>=25)
al3x
parents:
369
diff
changeset
|
430 } |
0 | 431 |
432 /* scan header */ | |
433 put_marker(&s->pb, SOS); | |
434 put_bits(&s->pb, 16, 12); /* length */ | |
435 put_bits(&s->pb, 8, 3); /* 3 components */ | |
436 | |
437 /* Y component */ | |
438 put_bits(&s->pb, 8, 1); /* index */ | |
439 put_bits(&s->pb, 4, 0); /* DC huffman table index */ | |
440 put_bits(&s->pb, 4, 0); /* AC huffman table index */ | |
441 | |
442 /* Cb component */ | |
443 put_bits(&s->pb, 8, 2); /* index */ | |
444 put_bits(&s->pb, 4, 1); /* DC huffman table index */ | |
445 put_bits(&s->pb, 4, 1); /* AC huffman table index */ | |
446 | |
447 /* Cr component */ | |
448 put_bits(&s->pb, 8, 3); /* index */ | |
449 put_bits(&s->pb, 4, 1); /* DC huffman table index */ | |
450 put_bits(&s->pb, 4, 1); /* AC huffman table index */ | |
451 | |
452 put_bits(&s->pb, 8, 0); /* Ss (not used) */ | |
453 put_bits(&s->pb, 8, 63); /* Se (not used) */ | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
454 put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */ |
0 | 455 } |
456 | |
457 void mjpeg_picture_trailer(MpegEncContext *s) | |
458 { | |
459 jflush_put_bits(&s->pb); | |
460 put_marker(&s->pb, EOI); | |
461 } | |
462 | |
463 static inline void encode_dc(MpegEncContext *s, int val, | |
464 UINT8 *huff_size, UINT16 *huff_code) | |
465 { | |
466 int mant, nbits; | |
467 | |
468 if (val == 0) { | |
469 jput_bits(&s->pb, huff_size[0], huff_code[0]); | |
470 } else { | |
471 mant = val; | |
472 if (val < 0) { | |
473 val = -val; | |
474 mant--; | |
475 } | |
476 | |
477 /* compute the log (XXX: optimize) */ | |
478 nbits = 0; | |
479 while (val != 0) { | |
480 val = val >> 1; | |
481 nbits++; | |
482 } | |
483 | |
484 jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]); | |
485 | |
486 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1)); | |
487 } | |
488 } | |
489 | |
490 static void encode_block(MpegEncContext *s, DCTELEM *block, int n) | |
491 { | |
492 int mant, nbits, code, i, j; | |
493 int component, dc, run, last_index, val; | |
494 MJpegContext *m = s->mjpeg_ctx; | |
495 UINT8 *huff_size_ac; | |
496 UINT16 *huff_code_ac; | |
497 | |
498 /* DC coef */ | |
499 component = (n <= 3 ? 0 : n - 4 + 1); | |
500 dc = block[0]; /* overflow is impossible */ | |
501 val = dc - s->last_dc[component]; | |
502 if (n < 4) { | |
503 encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance); | |
504 huff_size_ac = m->huff_size_ac_luminance; | |
505 huff_code_ac = m->huff_code_ac_luminance; | |
506 } else { | |
507 encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance); | |
508 huff_size_ac = m->huff_size_ac_chrominance; | |
509 huff_code_ac = m->huff_code_ac_chrominance; | |
510 } | |
511 s->last_dc[component] = dc; | |
512 | |
513 /* AC coefs */ | |
514 | |
515 run = 0; | |
516 last_index = s->block_last_index[n]; | |
517 for(i=1;i<=last_index;i++) { | |
518 j = zigzag_direct[i]; | |
519 val = block[j]; | |
520 if (val == 0) { | |
521 run++; | |
522 } else { | |
523 while (run >= 16) { | |
524 jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]); | |
525 run -= 16; | |
526 } | |
527 mant = val; | |
528 if (val < 0) { | |
529 val = -val; | |
530 mant--; | |
531 } | |
532 | |
533 /* compute the log (XXX: optimize) */ | |
534 nbits = 0; | |
535 while (val != 0) { | |
536 val = val >> 1; | |
537 nbits++; | |
538 } | |
539 code = (run << 4) | nbits; | |
540 | |
541 jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]); | |
542 | |
543 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1)); | |
544 run = 0; | |
545 } | |
546 } | |
547 | |
548 /* output EOB only if not already 64 values */ | |
549 if (last_index < 63 || run != 0) | |
550 jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]); | |
551 } | |
552 | |
553 void mjpeg_encode_mb(MpegEncContext *s, | |
554 DCTELEM block[6][64]) | |
555 { | |
556 int i; | |
557 for(i=0;i<6;i++) { | |
558 encode_block(s, block[i], i); | |
559 } | |
560 } | |
23 | 561 |
562 /******************************************/ | |
563 /* decoding */ | |
564 | |
565 /* compressed picture size */ | |
566 #define PICTURE_BUFFER_SIZE 100000 | |
567 | |
568 #define MAX_COMPONENTS 4 | |
569 | |
570 typedef struct MJpegDecodeContext { | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
571 AVCodecContext *avctx; |
23 | 572 GetBitContext gb; |
573 UINT32 header_state; | |
574 int start_code; /* current start code */ | |
575 UINT8 *buf_ptr; | |
576 int buffer_size; | |
577 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ | |
578 INT16 quant_matrixes[4][64]; | |
579 VLC vlcs[2][4]; | |
53 | 580 |
581 int org_width, org_height; /* size given at codec init */ | |
582 int first_picture; /* true if decoding first picture */ | |
583 int interlaced; /* true if interlaced */ | |
584 int bottom_field; /* true if bottom field */ | |
585 | |
23 | 586 int width, height; |
26 | 587 int nb_components; |
588 int component_id[MAX_COMPONENTS]; | |
23 | 589 int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */ |
590 int v_count[MAX_COMPONENTS]; | |
591 int h_max, v_max; /* maximum h and v counts */ | |
592 int quant_index[4]; /* quant table index for each component */ | |
593 int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */ | |
594 UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */ | |
595 int linesize[MAX_COMPONENTS]; | |
596 DCTELEM block[64] __align8; | |
597 UINT8 buffer[PICTURE_BUFFER_SIZE]; | |
354 | 598 |
599 int buggy_avid; | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
600 int restart_interval; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
601 int restart_count; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
602 int interleaved_rows; |
23 | 603 } MJpegDecodeContext; |
604 | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
605 #define SKIP_REMAINING(gb, len) { \ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
606 dprintf("reamining %d bytes in marker\n", len); \ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
607 if (len) while (--len) \ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
608 skip_bits(gb, 8); \ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
609 } |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
610 |
375 | 611 static int mjpeg_decode_dht(MJpegDecodeContext *s, UINT8 *buf, int buf_size); |
612 | |
29 | 613 static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table, |
614 int nb_codes) | |
615 { | |
616 UINT8 huff_size[256]; | |
617 UINT16 huff_code[256]; | |
618 | |
619 memset(huff_size, 0, sizeof(huff_size)); | |
620 build_huffman_codes(huff_size, huff_code, bits_table, val_table); | |
621 | |
622 init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2); | |
623 } | |
624 | |
23 | 625 static int mjpeg_decode_init(AVCodecContext *avctx) |
626 { | |
627 MJpegDecodeContext *s = avctx->priv_data; | |
628 | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
629 s->avctx = avctx; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
630 |
23 | 631 s->header_state = 0; |
632 s->mpeg_enc_ctx_allocated = 0; | |
633 s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into | |
634 account FF 00 case */ | |
635 s->start_code = -1; | |
636 s->buf_ptr = s->buffer; | |
53 | 637 s->first_picture = 1; |
638 s->org_width = avctx->width; | |
639 s->org_height = avctx->height; | |
29 | 640 |
641 build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12); | |
642 build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12); | |
643 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251); | |
644 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251); | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
645 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
646 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF) |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
647 { |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
648 printf("mjpeg: using external huffman table\n"); |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
649 mjpeg_decode_dht(s, avctx->extradata, avctx->extradata_size); |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
650 /* should check for error - but dunno */ |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
651 } |
23 | 652 return 0; |
653 } | |
654 | |
655 /* quantize tables */ | |
656 static int mjpeg_decode_dqt(MJpegDecodeContext *s, | |
657 UINT8 *buf, int buf_size) | |
658 { | |
37 | 659 int len, index, i, j; |
23 | 660 init_get_bits(&s->gb, buf, buf_size); |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
661 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
662 len = get_bits(&s->gb, 16) - 2; |
23 | 663 |
664 while (len >= 65) { | |
665 /* only 8 bit precision handled */ | |
666 if (get_bits(&s->gb, 4) != 0) | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
667 { |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
668 dprintf("dqt: 16bit precision\n"); |
23 | 669 return -1; |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
670 } |
23 | 671 index = get_bits(&s->gb, 4); |
672 if (index >= 4) | |
673 return -1; | |
674 dprintf("index=%d\n", index); | |
675 /* read quant table */ | |
37 | 676 for(i=0;i<64;i++) { |
677 j = zigzag_direct[i]; | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
678 s->quant_matrixes[index][j] = get_bits(&s->gb, 8); |
37 | 679 } |
23 | 680 len -= 65; |
681 } | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
682 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
683 SKIP_REMAINING(&s->gb, len); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
684 |
23 | 685 return 0; |
686 } | |
687 | |
688 /* decode huffman tables and build VLC decoders */ | |
689 static int mjpeg_decode_dht(MJpegDecodeContext *s, | |
690 UINT8 *buf, int buf_size) | |
691 { | |
692 int len, index, i, class, n, v, code_max; | |
693 UINT8 bits_table[17]; | |
694 UINT8 val_table[256]; | |
695 | |
696 init_get_bits(&s->gb, buf, buf_size); | |
697 | |
698 len = get_bits(&s->gb, 16); | |
699 len -= 2; | |
700 | |
701 while (len > 0) { | |
702 if (len < 17) | |
703 return -1; | |
704 class = get_bits(&s->gb, 4); | |
705 if (class >= 2) | |
706 return -1; | |
707 index = get_bits(&s->gb, 4); | |
708 if (index >= 4) | |
709 return -1; | |
710 n = 0; | |
711 for(i=1;i<=16;i++) { | |
712 bits_table[i] = get_bits(&s->gb, 8); | |
713 n += bits_table[i]; | |
714 } | |
715 len -= 17; | |
716 if (len < n || n > 256) | |
717 return -1; | |
718 | |
719 code_max = 0; | |
720 for(i=0;i<n;i++) { | |
721 v = get_bits(&s->gb, 8); | |
722 if (v > code_max) | |
723 code_max = v; | |
724 val_table[i] = v; | |
725 } | |
726 len -= n; | |
727 | |
728 /* build VLC and flush previous vlc if present */ | |
729 free_vlc(&s->vlcs[class][index]); | |
730 dprintf("class=%d index=%d nb_codes=%d\n", | |
731 class, index, code_max + 1); | |
29 | 732 build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1); |
23 | 733 } |
734 return 0; | |
735 } | |
736 | |
737 static int mjpeg_decode_sof0(MJpegDecodeContext *s, | |
738 UINT8 *buf, int buf_size) | |
739 { | |
26 | 740 int len, nb_components, i, width, height; |
23 | 741 |
742 init_get_bits(&s->gb, buf, buf_size); | |
743 | |
744 /* XXX: verify len field validity */ | |
745 len = get_bits(&s->gb, 16); | |
746 /* only 8 bits/component accepted */ | |
747 if (get_bits(&s->gb, 8) != 8) | |
748 return -1; | |
749 height = get_bits(&s->gb, 16); | |
750 width = get_bits(&s->gb, 16); | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
751 dprintf("sof0: picture: %dx%d\n", width, height); |
23 | 752 |
753 nb_components = get_bits(&s->gb, 8); | |
754 if (nb_components <= 0 || | |
755 nb_components > MAX_COMPONENTS) | |
756 return -1; | |
26 | 757 s->nb_components = nb_components; |
23 | 758 s->h_max = 1; |
759 s->v_max = 1; | |
760 for(i=0;i<nb_components;i++) { | |
761 /* component id */ | |
26 | 762 s->component_id[i] = get_bits(&s->gb, 8) - 1; |
23 | 763 s->h_count[i] = get_bits(&s->gb, 4); |
764 s->v_count[i] = get_bits(&s->gb, 4); | |
765 /* compute hmax and vmax (only used in interleaved case) */ | |
766 if (s->h_count[i] > s->h_max) | |
767 s->h_max = s->h_count[i]; | |
768 if (s->v_count[i] > s->v_max) | |
769 s->v_max = s->v_count[i]; | |
770 s->quant_index[i] = get_bits(&s->gb, 8); | |
771 if (s->quant_index[i] >= 4) | |
772 return -1; | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
773 dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i], |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
774 s->v_count[i], s->component_id[i], s->quant_index[i]); |
23 | 775 } |
776 | |
777 /* if different size, realloc/alloc picture */ | |
778 /* XXX: also check h_count and v_count */ | |
779 if (width != s->width || height != s->height) { | |
780 for(i=0;i<MAX_COMPONENTS;i++) { | |
781 free(s->current_picture[i]); | |
782 s->current_picture[i] = NULL; | |
783 } | |
784 s->width = width; | |
785 s->height = height; | |
53 | 786 /* test interlaced mode */ |
787 if (s->first_picture && | |
788 s->org_height != 0 && | |
789 s->height < ((s->org_height * 3) / 4)) { | |
790 s->interlaced = 1; | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
791 s->bottom_field = 0; |
53 | 792 } |
793 | |
23 | 794 for(i=0;i<nb_components;i++) { |
226 | 795 int w, h; |
796 w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max); | |
797 h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max); | |
798 w = w * 8 * s->h_count[i]; | |
799 h = h * 8 * s->v_count[i]; | |
53 | 800 if (s->interlaced) |
801 w *= 2; | |
23 | 802 s->linesize[i] = w; |
803 /* memory test is done in mjpeg_decode_sos() */ | |
804 s->current_picture[i] = av_mallocz(w * h); | |
805 } | |
53 | 806 s->first_picture = 0; |
23 | 807 } |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
808 |
354 | 809 if (len != (8+(3*nb_components))) |
810 { | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
811 dprintf("decode_sof0: error, len(%d) mismatch\n", len); |
354 | 812 } |
53 | 813 |
23 | 814 return 0; |
815 } | |
816 | |
817 static inline int decode_dc(MJpegDecodeContext *s, int dc_index) | |
818 { | |
819 int code, diff; | |
820 | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
821 code = get_vlc(&s->gb, &s->vlcs[0][dc_index]); |
23 | 822 if (code < 0) |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
823 { |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
824 dprintf("decode_dc: bad vlc: %d:%d (%x)\n", 0, dc_index, |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
825 &s->vlcs[0][dc_index]); |
23 | 826 return 0xffff; |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
827 } |
23 | 828 if (code == 0) { |
829 diff = 0; | |
830 } else { | |
831 diff = get_bits(&s->gb, code); | |
832 if ((diff & (1 << (code - 1))) == 0) | |
833 diff = (-1 << code) | (diff + 1); | |
834 } | |
835 return diff; | |
836 } | |
837 | |
838 /* decode block and dequantize */ | |
839 static int decode_block(MJpegDecodeContext *s, DCTELEM *block, | |
840 int component, int dc_index, int ac_index, int quant_index) | |
841 { | |
842 int nbits, code, i, j, level; | |
843 int run, val; | |
844 VLC *ac_vlc; | |
845 INT16 *quant_matrix; | |
846 | |
847 /* DC coef */ | |
848 val = decode_dc(s, dc_index); | |
849 if (val == 0xffff) { | |
850 dprintf("error dc\n"); | |
851 return -1; | |
852 } | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
853 quant_matrix = s->quant_matrixes[quant_index]; |
23 | 854 val = val * quant_matrix[0] + s->last_dc[component]; |
855 s->last_dc[component] = val; | |
856 block[0] = val; | |
857 /* AC coefs */ | |
858 ac_vlc = &s->vlcs[1][ac_index]; | |
859 i = 1; | |
860 for(;;) { | |
861 code = get_vlc(&s->gb, ac_vlc); | |
862 if (code < 0) { | |
863 dprintf("error ac\n"); | |
864 return -1; | |
865 } | |
866 /* EOB */ | |
867 if (code == 0) | |
868 break; | |
869 if (code == 0xf0) { | |
870 i += 16; | |
871 } else { | |
872 run = code >> 4; | |
873 nbits = code & 0xf; | |
874 level = get_bits(&s->gb, nbits); | |
875 if ((level & (1 << (nbits - 1))) == 0) | |
876 level = (-1 << nbits) | (level + 1); | |
877 i += run; | |
878 if (i >= 64) { | |
879 dprintf("error count: %d\n", i); | |
880 return -1; | |
881 } | |
882 j = zigzag_direct[i]; | |
883 block[j] = level * quant_matrix[j]; | |
884 i++; | |
28
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
885 if (i >= 64) |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
886 break; |
23 | 887 } |
888 } | |
889 return 0; | |
890 } | |
891 | |
892 static int mjpeg_decode_sos(MJpegDecodeContext *s, | |
893 UINT8 *buf, int buf_size) | |
894 { | |
47
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
895 int len, nb_components, i, j, n, h, v, ret; |
26 | 896 int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id; |
23 | 897 int comp_index[4]; |
898 int dc_index[4]; | |
899 int ac_index[4]; | |
900 int nb_blocks[4]; | |
901 int h_count[4]; | |
902 int v_count[4]; | |
903 | |
904 init_get_bits(&s->gb, buf, buf_size); | |
905 /* XXX: verify len field validity */ | |
906 len = get_bits(&s->gb, 16); | |
907 nb_components = get_bits(&s->gb, 8); | |
908 /* XXX: only interleaved scan accepted */ | |
909 if (nb_components != 3) | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
910 { |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
911 dprintf("decode_sos: components(%d) mismatch\n", nb_components); |
23 | 912 return -1; |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
913 } |
23 | 914 vmax = 0; |
915 hmax = 0; | |
916 for(i=0;i<nb_components;i++) { | |
26 | 917 id = get_bits(&s->gb, 8) - 1; |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
918 dprintf("component: %d\n", id); |
26 | 919 /* find component index */ |
920 for(index=0;index<s->nb_components;index++) | |
921 if (id == s->component_id[index]) | |
922 break; | |
923 if (index == s->nb_components) | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
924 { |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
925 dprintf("decode_sos: index(%d) out of components\n", index); |
23 | 926 return -1; |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
927 } |
26 | 928 |
23 | 929 comp_index[i] = index; |
930 nb_blocks[i] = s->h_count[index] * s->v_count[index]; | |
931 h_count[i] = s->h_count[index]; | |
932 v_count[i] = s->v_count[index]; | |
28
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
933 |
23 | 934 dc_index[i] = get_bits(&s->gb, 4); |
935 ac_index[i] = get_bits(&s->gb, 4); | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
936 |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
937 if (dc_index[i] < 0 || ac_index[i] < 0 || |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
938 dc_index[i] >= 4 || ac_index[i] >= 4) |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
939 goto out_of_range; |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
940 switch(s->start_code) |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
941 { |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
942 case SOF0: |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
943 if (dc_index[i] > 1 || ac_index[i] > 1) |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
944 goto out_of_range; |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
945 break; |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
946 case SOF1: |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
947 case SOF2: |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
948 if (dc_index[i] > 3 || ac_index[i] > 3) |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
949 goto out_of_range; |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
950 break; |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
951 case SOF3: |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
952 if (dc_index[i] > 3 || ac_index[i] != 0) |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
953 goto out_of_range; |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
954 break; |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
955 } |
23 | 956 } |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
957 skip_bits(&s->gb, 8); /* Ss */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
958 skip_bits(&s->gb, 8); /* Se */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
959 skip_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */ |
23 | 960 |
961 for(i=0;i<nb_components;i++) | |
962 s->last_dc[i] = 1024; | |
963 | |
964 if (nb_components > 1) { | |
965 /* interleaved stream */ | |
966 mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8); | |
967 mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8); | |
968 } else { | |
969 h = s->h_max / s->h_count[comp_index[0]]; | |
970 v = s->v_max / s->v_count[comp_index[0]]; | |
971 mb_width = (s->width + h * 8 - 1) / (h * 8); | |
972 mb_height = (s->height + v * 8 - 1) / (v * 8); | |
973 nb_blocks[0] = 1; | |
974 h_count[0] = 1; | |
975 v_count[0] = 1; | |
976 } | |
977 | |
978 for(mb_y = 0; mb_y < mb_height; mb_y++) { | |
979 for(mb_x = 0; mb_x < mb_width; mb_x++) { | |
980 for(i=0;i<nb_components;i++) { | |
981 UINT8 *ptr; | |
982 int x, y, c; | |
983 n = nb_blocks[i]; | |
984 c = comp_index[i]; | |
985 h = h_count[i]; | |
986 v = v_count[i]; | |
987 x = 0; | |
988 y = 0; | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
989 if (s->restart_interval && !s->restart_count) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
990 s->restart_count = s->restart_interval; |
23 | 991 for(j=0;j<n;j++) { |
992 memset(s->block, 0, sizeof(s->block)); | |
993 if (decode_block(s, s->block, i, | |
994 dc_index[i], ac_index[i], | |
995 s->quant_index[c]) < 0) { | |
996 dprintf("error %d %d\n", mb_y, mb_x); | |
47
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
997 ret = -1; |
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
998 goto the_end; |
23 | 999 } |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
1000 // dprintf("mb: %d %d processed\n", mb_y, mb_x); |
23 | 1001 ff_idct (s->block); |
1002 ptr = s->current_picture[c] + | |
1003 (s->linesize[c] * (v * mb_y + y) * 8) + | |
1004 (h * mb_x + x) * 8; | |
53 | 1005 if (s->interlaced && s->bottom_field) |
1006 ptr += s->linesize[c] >> 1; | |
23 | 1007 put_pixels_clamped(s->block, ptr, s->linesize[c]); |
1008 if (++x == h) { | |
1009 x = 0; | |
1010 y++; | |
1011 } | |
1012 } | |
1013 } | |
1014 } | |
1015 } | |
47
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
1016 ret = 0; |
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
1017 the_end: |
44
92d51f683931
added forgotten emms() - fix various segmentation faults when using mjpeg
glantau
parents:
37
diff
changeset
|
1018 emms_c(); |
47
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
1019 return ret; |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
1020 out_of_range: |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
1021 dprintf("decode_sos: ac/dc index out of range\n"); |
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
1022 return -1; |
23 | 1023 } |
1024 | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1025 static int mjpeg_decode_dri(MJpegDecodeContext *s, |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1026 UINT8 *buf, int buf_size) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1027 { |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1028 init_get_bits(&s->gb, buf, buf_size); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1029 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1030 if (get_bits(&s->gb, 16) != 4) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1031 return -1; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1032 s->restart_interval = get_bits(&s->gb, 16); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1033 printf("restart interval: %d\n", s->restart_interval); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1034 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1035 return 0; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1036 } |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1037 |
354 | 1038 #define FOURCC(a,b,c,d) ((a << 24) | (b << 16) | (c << 8) | d) |
1039 static int mjpeg_decode_app(MJpegDecodeContext *s, | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1040 UINT8 *buf, int buf_size, int start_code) |
354 | 1041 { |
1042 int len, id; | |
1043 | |
1044 init_get_bits(&s->gb, buf, buf_size); | |
1045 | |
1046 /* XXX: verify len field validity */ | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1047 len = get_bits(&s->gb, 16); |
354 | 1048 if (len < 5) |
1049 return -1; | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1050 |
354 | 1051 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1052 len -= 6; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1053 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1054 /* buggy AVID, it puts EOI only at every 10th frame */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1055 /* also this fourcc is used by non-avid files too, it means |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1056 interleaving, but it's always present in AVID files */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1057 if (id == FOURCC('A','V','I','1')) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1058 { |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1059 /* structure: |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1060 4bytes AVI1 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1061 1bytes polarity |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1062 1bytes always zero |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1063 4bytes field_size |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1064 4bytes field_size_less_padding |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1065 */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1066 s->buggy_avid = 1; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1067 if (s->first_picture) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1068 printf("mjpeg: workarounding buggy AVID\n"); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1069 s->interleaved_rows = get_bits(&s->gb, 8); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1070 #if 0 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1071 skip_bits(&s->gb, 8); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1072 skip_bits(&s->gb, 32); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1073 skip_bits(&s->gb, 32); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1074 len -= 10; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1075 #endif |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1076 if (s->interleaved_rows) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1077 printf("mjpeg: interleaved rows: %d\n", s->interleaved_rows); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1078 goto out; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1079 } |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1080 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1081 len -= 2; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1082 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1083 if (id == FOURCC('J','F','I','F')) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1084 { |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1085 skip_bits(&s->gb, 8); /* the trailing zero-byte */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1086 printf("mjpeg: JFIF header found (version: %x.%x)\n", |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1087 get_bits(&s->gb, 8), get_bits(&s->gb, 8)); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1088 goto out; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1089 } |
354 | 1090 |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1091 /* Apple MJPEG-A */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1092 if ((start_code == APP1) && (len > (0x28 - 8))) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1093 { |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1094 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1095 len -= 4; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1096 if (id == FOURCC('m','j','p','g')) /* Apple MJPEG-A */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1097 { |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1098 #if 0 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1099 skip_bits(&s->gb, 32); /* field size */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1100 skip_bits(&s->gb, 32); /* pad field size */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1101 skip_bits(&s->gb, 32); /* next off */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1102 skip_bits(&s->gb, 32); /* quant off */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1103 skip_bits(&s->gb, 32); /* huff off */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1104 skip_bits(&s->gb, 32); /* image off */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1105 skip_bits(&s->gb, 32); /* scan off */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1106 skip_bits(&s->gb, 32); /* data off */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1107 #endif |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1108 if (s->first_picture) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1109 printf("mjpeg: Apple MJPEG-A header found\n"); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1110 } |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1111 } |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1112 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1113 out: |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1114 /* should check for further values.. */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1115 SKIP_REMAINING(&s->gb, len); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1116 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1117 return 0; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1118 } |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1119 #undef FOURCC |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1120 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1121 static int mjpeg_decode_com(MJpegDecodeContext *s, |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1122 UINT8 *buf, int buf_size) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1123 { |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1124 int len, i; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1125 UINT8 *cbuf; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1126 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1127 init_get_bits(&s->gb, buf, buf_size); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1128 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1129 /* XXX: verify len field validity */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1130 len = get_bits(&s->gb, 16)-2; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1131 cbuf = malloc(len+1); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1132 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1133 for (i = 0; i < len; i++) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1134 cbuf[i] = get_bits(&s->gb, 8); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1135 if (cbuf[i-1] == '\n') |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1136 cbuf[i-1] = 0; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1137 else |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1138 cbuf[i] = 0; |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1139 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1140 printf("mjpeg comment: '%s'\n", cbuf); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1141 |
354 | 1142 /* buggy avid, it puts EOI only at every 10th frame */ |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1143 if (!strcmp(cbuf, "AVID")) |
354 | 1144 { |
1145 s->buggy_avid = 1; | |
1146 if (s->first_picture) | |
1147 printf("mjpeg: workarounding buggy AVID\n"); | |
1148 } | |
1149 | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1150 free(cbuf); |
354 | 1151 |
1152 return 0; | |
1153 } | |
1154 | |
23 | 1155 /* return the 8 bit start code value and update the search |
1156 state. Return -1 if no start code found */ | |
1157 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end, | |
1158 UINT32 *header_state) | |
1159 { | |
1160 UINT8 *buf_ptr; | |
1161 unsigned int state, v; | |
1162 int val; | |
1163 | |
1164 state = *header_state; | |
1165 buf_ptr = *pbuf_ptr; | |
1166 if (state) { | |
1167 /* get marker */ | |
1168 found: | |
1169 if (buf_ptr < buf_end) { | |
1170 val = *buf_ptr++; | |
1171 state = 0; | |
1172 } else { | |
1173 val = -1; | |
1174 } | |
1175 } else { | |
1176 while (buf_ptr < buf_end) { | |
1177 v = *buf_ptr++; | |
1178 if (v == 0xff) { | |
1179 state = 1; | |
1180 goto found; | |
1181 } | |
1182 } | |
1183 val = -1; | |
1184 } | |
1185 *pbuf_ptr = buf_ptr; | |
1186 *header_state = state; | |
1187 return val; | |
1188 } | |
1189 | |
1190 static int mjpeg_decode_frame(AVCodecContext *avctx, | |
1191 void *data, int *data_size, | |
1192 UINT8 *buf, int buf_size) | |
1193 { | |
1194 MJpegDecodeContext *s = avctx->priv_data; | |
1195 UINT8 *buf_end, *buf_ptr, *buf_start; | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
1196 int len, code, input_size, i; |
23 | 1197 AVPicture *picture = data; |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
1198 unsigned int start_code; |
23 | 1199 |
68 | 1200 *data_size = 0; |
1201 | |
23 | 1202 /* no supplementary picture */ |
68 | 1203 if (buf_size == 0) |
23 | 1204 return 0; |
1205 | |
1206 buf_ptr = buf; | |
1207 buf_end = buf + buf_size; | |
1208 while (buf_ptr < buf_end) { | |
1209 buf_start = buf_ptr; | |
1210 /* find start next marker */ | |
1211 code = find_marker(&buf_ptr, buf_end, &s->header_state); | |
1212 /* copy to buffer */ | |
1213 len = buf_ptr - buf_start; | |
1214 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) { | |
1215 /* data too big : flush */ | |
1216 s->buf_ptr = s->buffer; | |
1217 if (code > 0) | |
1218 s->start_code = code; | |
1219 } else { | |
1220 memcpy(s->buf_ptr, buf_start, len); | |
1221 s->buf_ptr += len; | |
1222 /* if we got FF 00, we copy FF to the stream to unescape FF 00 */ | |
349
34f6c77ff01a
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
arpi_esp
parents:
344
diff
changeset
|
1223 /* valid marker code is between 00 and ff - alex */ |
354 | 1224 if (code <= 0 || code >= 0xff) { |
23 | 1225 s->buf_ptr--; |
354 | 1226 } else { |
23 | 1227 /* prepare data for next start code */ |
1228 input_size = s->buf_ptr - s->buffer; | |
1229 start_code = s->start_code; | |
1230 s->buf_ptr = s->buffer; | |
1231 s->start_code = code; | |
44
92d51f683931
added forgotten emms() - fix various segmentation faults when using mjpeg
glantau
parents:
37
diff
changeset
|
1232 dprintf("marker=%x\n", start_code); |
23 | 1233 switch(start_code) { |
1234 case SOI: | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1235 s->restart_interval = 0; |
23 | 1236 /* nothing to do on SOI */ |
1237 break; | |
1238 case DQT: | |
1239 mjpeg_decode_dqt(s, s->buffer, input_size); | |
1240 break; | |
1241 case DHT: | |
1242 mjpeg_decode_dht(s, s->buffer, input_size); | |
1243 break; | |
1244 case SOF0: | |
1245 mjpeg_decode_sof0(s, s->buffer, input_size); | |
1246 break; | |
1247 case SOS: | |
1248 mjpeg_decode_sos(s, s->buffer, input_size); | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1249 if (s->start_code == EOI || s->buggy_avid || s->restart_interval) { |
53 | 1250 int l; |
1251 if (s->interlaced) { | |
1252 s->bottom_field ^= 1; | |
1253 /* if not bottom field, do not output image yet */ | |
1254 if (s->bottom_field) | |
1255 goto the_end; | |
1256 } | |
23 | 1257 for(i=0;i<3;i++) { |
1258 picture->data[i] = s->current_picture[i]; | |
53 | 1259 l = s->linesize[i]; |
1260 if (s->interlaced) | |
1261 l >>= 1; | |
1262 picture->linesize[i] = l; | |
23 | 1263 } |
1264 *data_size = sizeof(AVPicture); | |
1265 avctx->height = s->height; | |
53 | 1266 if (s->interlaced) |
1267 avctx->height *= 2; | |
23 | 1268 avctx->width = s->width; |
28
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1269 /* XXX: not complete test ! */ |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1270 switch((s->h_count[0] << 4) | s->v_count[0]) { |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1271 case 0x11: |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1272 avctx->pix_fmt = PIX_FMT_YUV444P; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1273 break; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1274 case 0x21: |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1275 avctx->pix_fmt = PIX_FMT_YUV422P; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1276 break; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1277 default: |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1278 case 0x22: |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1279 avctx->pix_fmt = PIX_FMT_YUV420P; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1280 break; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
1281 } |
47
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
1282 /* dummy quality */ |
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
1283 /* XXX: infer it with matrix */ |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1284 avctx->quality = 3; |
23 | 1285 goto the_end; |
1286 } | |
1287 break; | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1288 case DRI: |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1289 mjpeg_decode_dri(s, s->buffer, input_size); |
354 | 1290 break; |
1291 case SOF1: | |
1292 case SOF2: | |
1293 case SOF3: | |
1294 case SOF5: | |
1295 case SOF6: | |
1296 case SOF7: | |
1297 case SOF9: | |
1298 case SOF10: | |
1299 case SOF11: | |
1300 case SOF13: | |
1301 case SOF14: | |
1302 case SOF15: | |
1303 case JPG: | |
1304 printf("mjpeg: unsupported coding type (%x)\n", start_code); | |
1305 return -1; | |
23 | 1306 } |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1307 #if 1 |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1308 if (start_code >= 0xd0 && start_code <= 0xd7) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1309 { |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1310 dprintf("restart marker: %d\n", start_code&0x0f); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1311 } |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1312 else if (s->first_picture) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1313 { |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1314 /* APP fields */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1315 if (start_code >= 0xe0 && start_code <= 0xef) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1316 mjpeg_decode_app(s, s->buffer, input_size, start_code); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1317 /* Comment */ |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1318 else if (start_code == COM) |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1319 mjpeg_decode_com(s, s->buffer, input_size); |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1320 } |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1321 #endif |
23 | 1322 } |
1323 } | |
1324 } | |
1325 the_end: | |
1326 return buf_ptr - buf; | |
1327 } | |
1328 | |
1329 static int mjpeg_decode_end(AVCodecContext *avctx) | |
1330 { | |
1331 MJpegDecodeContext *s = avctx->priv_data; | |
1332 int i, j; | |
1333 | |
1334 for(i=0;i<MAX_COMPONENTS;i++) | |
1335 free(s->current_picture[i]); | |
1336 for(i=0;i<2;i++) { | |
1337 for(j=0;j<4;j++) | |
1338 free_vlc(&s->vlcs[i][j]); | |
1339 } | |
1340 return 0; | |
1341 } | |
1342 | |
1343 AVCodec mjpeg_decoder = { | |
1344 "mjpeg", | |
1345 CODEC_TYPE_VIDEO, | |
1346 CODEC_ID_MJPEG, | |
1347 sizeof(MJpegDecodeContext), | |
1348 mjpeg_decode_init, | |
1349 NULL, | |
1350 mjpeg_decode_end, | |
1351 mjpeg_decode_frame, | |
369
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1352 0, |
c60869851bb4
added support for various app headers, and writin FFmpeg comment
al3x
parents:
357
diff
changeset
|
1353 NULL |
23 | 1354 }; |