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