annotate mjpegenc.c @ 15:076bcac8d059 libavcodec

added cpuid.s
author glantau
date Mon, 30 Jul 2001 23:50:04 +0000
parents 986e461dc072
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 * MJPEG encoder
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 * Copyright (c) 2000 Gerard Lantau.
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 * (at your option) any later version.
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * GNU General Public License for more details.
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 * along with this program; if not, write to the Free Software
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
19 #include <stdlib.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 #include <stdio.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
21 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 #include "dsputil.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
23 #include "mpegvideo.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
24
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 typedef struct MJpegContext {
986e461dc072 Initial revision
glantau
parents:
diff changeset
26 UINT8 huff_size_dc_luminance[12];
986e461dc072 Initial revision
glantau
parents:
diff changeset
27 UINT16 huff_code_dc_luminance[12];
986e461dc072 Initial revision
glantau
parents:
diff changeset
28 UINT8 huff_size_dc_chrominance[12];
986e461dc072 Initial revision
glantau
parents:
diff changeset
29 UINT16 huff_code_dc_chrominance[12];
986e461dc072 Initial revision
glantau
parents:
diff changeset
30
986e461dc072 Initial revision
glantau
parents:
diff changeset
31 UINT8 huff_size_ac_luminance[256];
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 UINT16 huff_code_ac_luminance[256];
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 UINT8 huff_size_ac_chrominance[256];
986e461dc072 Initial revision
glantau
parents:
diff changeset
34 UINT16 huff_code_ac_chrominance[256];
986e461dc072 Initial revision
glantau
parents:
diff changeset
35 } MJpegContext;
986e461dc072 Initial revision
glantau
parents:
diff changeset
36
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 #define SOF0 0xc0
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 #define SOI 0xd8
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 #define EOI 0xd9
986e461dc072 Initial revision
glantau
parents:
diff changeset
40 #define DQT 0xdb
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 #define DHT 0xc4
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 #define SOS 0xda
986e461dc072 Initial revision
glantau
parents:
diff changeset
43
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 /* These are the sample quantization tables given in JPEG spec section K.1.
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 * The spec says that the values given produce "good" quality, and
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 * when divided by 2, "very good" quality.
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 static const unsigned char std_luminance_quant_tbl[64] = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 16, 11, 10, 16, 24, 40, 51, 61,
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 12, 12, 14, 19, 26, 58, 60, 55,
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 14, 13, 16, 24, 40, 57, 69, 56,
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 14, 17, 22, 29, 51, 87, 80, 62,
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 18, 22, 37, 56, 68, 109, 103, 77,
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 24, 35, 55, 64, 81, 104, 113, 92,
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 49, 64, 78, 87, 103, 121, 120, 101,
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 72, 92, 95, 98, 112, 100, 103, 99
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 static const unsigned char std_chrominance_quant_tbl[64] = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 17, 18, 24, 47, 99, 99, 99, 99,
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 18, 21, 26, 66, 99, 99, 99, 99,
986e461dc072 Initial revision
glantau
parents:
diff changeset
62 24, 26, 56, 99, 99, 99, 99, 99,
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 47, 66, 99, 99, 99, 99, 99, 99,
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 99, 99, 99, 99, 99, 99, 99, 99,
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 99, 99, 99, 99, 99, 99, 99, 99,
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 99, 99, 99, 99, 99, 99, 99, 99,
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 99, 99, 99, 99, 99, 99, 99, 99
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
70
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 /* IMPORTANT: these are only valid for 8-bit data precision! */
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 static const UINT8 bits_dc_luminance[17] =
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 static const UINT8 val_dc_luminance[] =
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
77
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 static const UINT8 bits_dc_chrominance[17] =
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 static const UINT8 val_dc_chrominance[] =
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
82
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 static const UINT8 bits_ac_luminance[17] =
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 static const UINT8 val_ac_luminance[] =
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 0xf9, 0xfa
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
108
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 static const UINT8 bits_ac_chrominance[17] =
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
111
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 static const UINT8 val_ac_chrominance[] =
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 0xf9, 0xfa
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
135
986e461dc072 Initial revision
glantau
parents:
diff changeset
136
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 /* isn't this function nicer than the one in the libjpeg ? */
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code,
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 const UINT8 *bits_table, const UINT8 *val_table)
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 int i, j, k,nb, code, sym;
986e461dc072 Initial revision
glantau
parents:
diff changeset
142
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 code = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 k = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 for(i=1;i<=16;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 nb = bits_table[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 for(j=0;j<nb;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 sym = val_table[k++];
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 huff_size[sym] = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 huff_code[sym] = code;
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 code++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 code <<= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
156
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 int mjpeg_init(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 MJpegContext *m;
986e461dc072 Initial revision
glantau
parents:
diff changeset
160
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 m = malloc(sizeof(MJpegContext));
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 if (!m)
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
164
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 /* build all the huffman tables */
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 build_huffman_codes(m->huff_size_dc_luminance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 m->huff_code_dc_luminance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 bits_dc_luminance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 val_dc_luminance);
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 build_huffman_codes(m->huff_size_dc_chrominance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 m->huff_code_dc_chrominance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 bits_dc_chrominance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 val_dc_chrominance);
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 build_huffman_codes(m->huff_size_ac_luminance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 m->huff_code_ac_luminance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 bits_ac_luminance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 val_ac_luminance);
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 build_huffman_codes(m->huff_size_ac_chrominance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 m->huff_code_ac_chrominance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 bits_ac_chrominance,
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 val_ac_chrominance);
986e461dc072 Initial revision
glantau
parents:
diff changeset
182
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 s->mjpeg_ctx = m;
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
186
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 void mjpeg_close(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 free(s->mjpeg_ctx);
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
191
986e461dc072 Initial revision
glantau
parents:
diff changeset
192 static inline void put_marker(PutBitContext *p, int code)
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 put_bits(p, 8, 0xff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 put_bits(p, 8, code);
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
197
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 /* table_class: 0 = DC coef, 1 = AC coefs */
986e461dc072 Initial revision
glantau
parents:
diff changeset
199 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 const UINT8 *bits_table, const UINT8 *value_table)
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 PutBitContext *p = &s->pb;
986e461dc072 Initial revision
glantau
parents:
diff changeset
203 int n, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
204
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 put_bits(p, 4, table_class);
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 put_bits(p, 4, table_id);
986e461dc072 Initial revision
glantau
parents:
diff changeset
207
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 n = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 for(i=1;i<=16;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
210 n += bits_table[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 put_bits(p, 8, bits_table[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
213
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 for(i=0;i<n;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 put_bits(p, 8, value_table[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
216
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 return n + 17;
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
219
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 static void jpeg_table_header(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 PutBitContext *p = &s->pb;
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 int i, size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 UINT8 *ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
225
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 /* quant matrixes */
986e461dc072 Initial revision
glantau
parents:
diff changeset
227 put_marker(p, DQT);
986e461dc072 Initial revision
glantau
parents:
diff changeset
228 put_bits(p, 16, 2 + 1 * (1 + 64));
986e461dc072 Initial revision
glantau
parents:
diff changeset
229 put_bits(p, 4, 0); /* 8 bit precision */
986e461dc072 Initial revision
glantau
parents:
diff changeset
230 put_bits(p, 4, 0); /* table 0 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 for(i=0;i<64;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 put_bits(p, 8, s->intra_matrix[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
234 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 put_bits(p, 4, 0); /* 8 bit precision */
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 put_bits(p, 4, 1); /* table 1 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 for(i=0;i<64;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 put_bits(p, 8, s->chroma_intra_matrix[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
241
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 /* huffman table */
986e461dc072 Initial revision
glantau
parents:
diff changeset
243 put_marker(p, DHT);
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 flush_put_bits(p);
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 ptr = p->buf_ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 put_bits(p, 16, 0); /* patched later */
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 size = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
986e461dc072 Initial revision
glantau
parents:
diff changeset
250
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
986e461dc072 Initial revision
glantau
parents:
diff changeset
253 ptr[0] = size >> 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 ptr[1] = size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
255 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
256
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 void mjpeg_picture_header(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
258 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 put_marker(&s->pb, SOI);
986e461dc072 Initial revision
glantau
parents:
diff changeset
260
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 jpeg_table_header(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
262
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 put_marker(&s->pb, SOF0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
264
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 put_bits(&s->pb, 16, 17);
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 put_bits(&s->pb, 8, 8); /* 8 bits/component */
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 put_bits(&s->pb, 16, s->height);
986e461dc072 Initial revision
glantau
parents:
diff changeset
268 put_bits(&s->pb, 16, s->width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 put_bits(&s->pb, 8, 3); /* 3 components */
986e461dc072 Initial revision
glantau
parents:
diff changeset
270
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 /* Y component */
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 put_bits(&s->pb, 8, 1); /* component number */
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 put_bits(&s->pb, 4, 2); /* H factor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 put_bits(&s->pb, 4, 2); /* V factor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 put_bits(&s->pb, 8, 0); /* select matrix */
986e461dc072 Initial revision
glantau
parents:
diff changeset
276
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 /* Cb component */
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 put_bits(&s->pb, 8, 2); /* component number */
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 put_bits(&s->pb, 4, 1); /* H factor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 put_bits(&s->pb, 4, 1); /* V factor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 put_bits(&s->pb, 8, 0); /* select matrix */
986e461dc072 Initial revision
glantau
parents:
diff changeset
282
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 /* Cr component */
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 put_bits(&s->pb, 8, 3); /* component number */
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 put_bits(&s->pb, 4, 1); /* H factor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 put_bits(&s->pb, 4, 1); /* V factor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 put_bits(&s->pb, 8, 0); /* select matrix */
986e461dc072 Initial revision
glantau
parents:
diff changeset
288
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 /* scan header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 put_marker(&s->pb, SOS);
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 put_bits(&s->pb, 16, 12); /* length */
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 put_bits(&s->pb, 8, 3); /* 3 components */
986e461dc072 Initial revision
glantau
parents:
diff changeset
293
986e461dc072 Initial revision
glantau
parents:
diff changeset
294 /* Y component */
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 put_bits(&s->pb, 8, 1); /* index */
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 put_bits(&s->pb, 4, 0); /* DC huffman table index */
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 put_bits(&s->pb, 4, 0); /* AC huffman table index */
986e461dc072 Initial revision
glantau
parents:
diff changeset
298
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 /* Cb component */
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 put_bits(&s->pb, 8, 2); /* index */
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 put_bits(&s->pb, 4, 1); /* DC huffman table index */
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 put_bits(&s->pb, 4, 1); /* AC huffman table index */
986e461dc072 Initial revision
glantau
parents:
diff changeset
303
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 /* Cr component */
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 put_bits(&s->pb, 8, 3); /* index */
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 put_bits(&s->pb, 4, 1); /* DC huffman table index */
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 put_bits(&s->pb, 4, 1); /* AC huffman table index */
986e461dc072 Initial revision
glantau
parents:
diff changeset
308
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 put_bits(&s->pb, 8, 0); /* Ss (not used) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 put_bits(&s->pb, 8, 63); /* Se (not used) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 put_bits(&s->pb, 8, 0); /* (not used) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
312 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
313
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 void mjpeg_picture_trailer(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 jflush_put_bits(&s->pb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 put_marker(&s->pb, EOI);
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
319
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 static inline void encode_dc(MpegEncContext *s, int val,
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 UINT8 *huff_size, UINT16 *huff_code)
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 int mant, nbits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
324
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 if (val == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 jput_bits(&s->pb, huff_size[0], huff_code[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
327 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 mant = val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
329 if (val < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
330 val = -val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
331 mant--;
986e461dc072 Initial revision
glantau
parents:
diff changeset
332 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
333
986e461dc072 Initial revision
glantau
parents:
diff changeset
334 /* compute the log (XXX: optimize) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
335 nbits = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
336 while (val != 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
337 val = val >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 nbits++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
339 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
340
986e461dc072 Initial revision
glantau
parents:
diff changeset
341 jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
342
986e461dc072 Initial revision
glantau
parents:
diff changeset
343 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
344 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
345 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
346
986e461dc072 Initial revision
glantau
parents:
diff changeset
347 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
348 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
349 int mant, nbits, code, i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
350 int component, dc, run, last_index, val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
351 MJpegContext *m = s->mjpeg_ctx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
352 UINT8 *huff_size_ac;
986e461dc072 Initial revision
glantau
parents:
diff changeset
353 UINT16 *huff_code_ac;
986e461dc072 Initial revision
glantau
parents:
diff changeset
354
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 /* DC coef */
986e461dc072 Initial revision
glantau
parents:
diff changeset
356 component = (n <= 3 ? 0 : n - 4 + 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 dc = block[0]; /* overflow is impossible */
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 val = dc - s->last_dc[component];
986e461dc072 Initial revision
glantau
parents:
diff changeset
359 if (n < 4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
360 encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
986e461dc072 Initial revision
glantau
parents:
diff changeset
361 huff_size_ac = m->huff_size_ac_luminance;
986e461dc072 Initial revision
glantau
parents:
diff changeset
362 huff_code_ac = m->huff_code_ac_luminance;
986e461dc072 Initial revision
glantau
parents:
diff changeset
363 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
364 encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
986e461dc072 Initial revision
glantau
parents:
diff changeset
365 huff_size_ac = m->huff_size_ac_chrominance;
986e461dc072 Initial revision
glantau
parents:
diff changeset
366 huff_code_ac = m->huff_code_ac_chrominance;
986e461dc072 Initial revision
glantau
parents:
diff changeset
367 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
368 s->last_dc[component] = dc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
369
986e461dc072 Initial revision
glantau
parents:
diff changeset
370 /* AC coefs */
986e461dc072 Initial revision
glantau
parents:
diff changeset
371
986e461dc072 Initial revision
glantau
parents:
diff changeset
372 run = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
373 last_index = s->block_last_index[n];
986e461dc072 Initial revision
glantau
parents:
diff changeset
374 for(i=1;i<=last_index;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
375 j = zigzag_direct[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
376 val = block[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
377 if (val == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
378 run++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
379 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
380 while (run >= 16) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
381 jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
382 run -= 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
383 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
384 mant = val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
385 if (val < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
386 val = -val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
387 mant--;
986e461dc072 Initial revision
glantau
parents:
diff changeset
388 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
389
986e461dc072 Initial revision
glantau
parents:
diff changeset
390 /* compute the log (XXX: optimize) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
391 nbits = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
392 while (val != 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
393 val = val >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
394 nbits++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
395 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
396 code = (run << 4) | nbits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
397
986e461dc072 Initial revision
glantau
parents:
diff changeset
398 jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
399
986e461dc072 Initial revision
glantau
parents:
diff changeset
400 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
401 run = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
402 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
403 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
404
986e461dc072 Initial revision
glantau
parents:
diff changeset
405 /* output EOB only if not already 64 values */
986e461dc072 Initial revision
glantau
parents:
diff changeset
406 if (last_index < 63 || run != 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
407 jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
408 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
409
986e461dc072 Initial revision
glantau
parents:
diff changeset
410 void mjpeg_encode_mb(MpegEncContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
411 DCTELEM block[6][64])
986e461dc072 Initial revision
glantau
parents:
diff changeset
412 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
413 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
414 for(i=0;i<6;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
415 encode_block(s, block[i], i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
416 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
417 }