annotate mjpegenc.c @ 5373:69eac3e47b9a libavcodec

remove portions of bit allocation which are no longer needed since they are shared with the encoder
author jbr
date Thu, 19 Jul 2007 00:50:31 +0000
parents 470601203f44
children 48759bfbd073
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1 /*
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
2 * MJPEG encoder
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
4 * Copyright (c) 2003 Alex Beregszaszi
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
5 * Copyright (c) 2003-2004 Michael Niedermayer
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
6 *
5214
470601203f44 Group all copyright and author notices together.
diego
parents: 5089
diff changeset
7 * Support for external huffman table, various fixes (AVID workaround),
470601203f44 Group all copyright and author notices together.
diego
parents: 5089
diff changeset
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support
470601203f44 Group all copyright and author notices together.
diego
parents: 5089
diff changeset
9 * by Alex Beregszaszi
470601203f44 Group all copyright and author notices together.
diego
parents: 5089
diff changeset
10 *
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
11 * This file is part of FFmpeg.
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
12 *
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
13 * FFmpeg is free software; you can redistribute it and/or
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
14 * modify it under the terms of the GNU Lesser General Public
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
15 * License as published by the Free Software Foundation; either
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
16 * version 2.1 of the License, or (at your option) any later version.
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
17 *
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
18 * FFmpeg is distributed in the hope that it will be useful,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
21 * Lesser General Public License for more details.
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
22 *
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
23 * You should have received a copy of the GNU Lesser General Public
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
24 * License along with FFmpeg; if not, write to the Free Software
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
26 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
27
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
28 /**
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
29 * @file mjpegenc.c
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
30 * MJPEG encoder.
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
31 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
32
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
33 //#define DEBUG
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
34 #include <assert.h>
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
35
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
36 #include "avcodec.h"
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
37 #include "dsputil.h"
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
38 #include "mpegvideo.h"
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
39 #include "mjpeg.h"
5028
3d8a813666e4 split ljpeg encoder out of mjpeg.c
aurel
parents: 5021
diff changeset
40 #include "mjpegenc.h"
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
41
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
42 /* use two quantizer tables (one for luminance and one for chrominance) */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
43 /* not yet working */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
44 #undef TWOMATRIXES
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
45
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
46
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
47 int ff_mjpeg_encode_init(MpegEncContext *s)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
48 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
49 MJpegContext *m;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
50
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
51 m = av_malloc(sizeof(MJpegContext));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
52 if (!m)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
53 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
54
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
55 s->min_qcoeff=-1023;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
56 s->max_qcoeff= 1023;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
57
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
58 /* build all the huffman tables */
5021
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
59 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
60 m->huff_code_dc_luminance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
61 ff_mjpeg_bits_dc_luminance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
62 ff_mjpeg_val_dc_luminance);
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
63 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
64 m->huff_code_dc_chrominance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
65 ff_mjpeg_bits_dc_chrominance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
66 ff_mjpeg_val_dc_chrominance);
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
67 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
68 m->huff_code_ac_luminance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
69 ff_mjpeg_bits_ac_luminance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
70 ff_mjpeg_val_ac_luminance);
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
71 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
72 m->huff_code_ac_chrominance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
73 ff_mjpeg_bits_ac_chrominance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
74 ff_mjpeg_val_ac_chrominance);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
75
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
76 s->mjpeg_ctx = m;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
77 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
78 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
79
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
80 void ff_mjpeg_encode_close(MpegEncContext *s)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
81 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
82 av_free(s->mjpeg_ctx);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
83 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
84
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
85 /* table_class: 0 = DC coef, 1 = AC coefs */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
86 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
87 const uint8_t *bits_table, const uint8_t *value_table)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
88 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
89 PutBitContext *p = &s->pb;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
90 int n, i;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
91
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
92 put_bits(p, 4, table_class);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
93 put_bits(p, 4, table_id);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
94
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
95 n = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
96 for(i=1;i<=16;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
97 n += bits_table[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
98 put_bits(p, 8, bits_table[i]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
99 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
100
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
101 for(i=0;i<n;i++)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
102 put_bits(p, 8, value_table[i]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
103
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
104 return n + 17;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
105 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
106
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
107 static void jpeg_table_header(MpegEncContext *s)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
108 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
109 PutBitContext *p = &s->pb;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
110 int i, j, size;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
111 uint8_t *ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
112
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
113 /* quant matrixes */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
114 put_marker(p, DQT);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
115 #ifdef TWOMATRIXES
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
116 put_bits(p, 16, 2 + 2 * (1 + 64));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
117 #else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
118 put_bits(p, 16, 2 + 1 * (1 + 64));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
119 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
120 put_bits(p, 4, 0); /* 8 bit precision */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
121 put_bits(p, 4, 0); /* table 0 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
122 for(i=0;i<64;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
123 j = s->intra_scantable.permutated[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
124 put_bits(p, 8, s->intra_matrix[j]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
125 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
126 #ifdef TWOMATRIXES
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
127 put_bits(p, 4, 0); /* 8 bit precision */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
128 put_bits(p, 4, 1); /* table 1 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
129 for(i=0;i<64;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
130 j = s->intra_scantable.permutated[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
131 put_bits(p, 8, s->chroma_intra_matrix[j]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
132 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
133 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
134
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
135 /* huffman table */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
136 put_marker(p, DHT);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
137 flush_put_bits(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
138 ptr = pbBufPtr(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
139 put_bits(p, 16, 0); /* patched later */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
140 size = 2;
5021
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
141 size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
142 ff_mjpeg_val_dc_luminance);
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
143 size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
144 ff_mjpeg_val_dc_chrominance);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
145
5021
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
146 size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
147 ff_mjpeg_val_ac_luminance);
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
148 size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
149 ff_mjpeg_val_ac_chrominance);
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 5030
diff changeset
150 AV_WB16(ptr, size);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
151 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
152
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
153 static void jpeg_put_comments(MpegEncContext *s)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
154 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
155 PutBitContext *p = &s->pb;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
156 int size;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
157 uint8_t *ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
158
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
159 if (s->aspect_ratio_info /* && !lossless */)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
160 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
161 /* JFIF header */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
162 put_marker(p, APP0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
163 put_bits(p, 16, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
164 ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
165 put_bits(p, 16, 0x0201); /* v 1.02 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
166 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
167 put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
168 put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
169 put_bits(p, 8, 0); /* thumbnail width */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
170 put_bits(p, 8, 0); /* thumbnail height */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
171 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
172
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
173 /* comment */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
174 if(!(s->flags & CODEC_FLAG_BITEXACT)){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
175 put_marker(p, COM);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
176 flush_put_bits(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
177 ptr = pbBufPtr(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
178 put_bits(p, 16, 0); /* patched later */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
179 ff_put_string(p, LIBAVCODEC_IDENT, 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
180 size = strlen(LIBAVCODEC_IDENT)+3;
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 5030
diff changeset
181 AV_WB16(ptr, size);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
182 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
183
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
184 if( s->avctx->pix_fmt == PIX_FMT_YUV420P
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
185 ||s->avctx->pix_fmt == PIX_FMT_YUV422P
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
186 ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
187 put_marker(p, COM);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
188 flush_put_bits(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
189 ptr = pbBufPtr(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
190 put_bits(p, 16, 0); /* patched later */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
191 ff_put_string(p, "CS=ITU601", 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
192 size = strlen("CS=ITU601")+3;
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 5030
diff changeset
193 AV_WB16(ptr, size);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
194 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
195 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
196
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
197 void ff_mjpeg_encode_picture_header(MpegEncContext *s)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
198 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
199 const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
200
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
201 put_marker(&s->pb, SOI);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
202
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
203 jpeg_put_comments(s);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
204
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
205 jpeg_table_header(s);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
206
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
207 switch(s->avctx->codec_id){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
208 case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
209 case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
210 default: assert(0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
211 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
212
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
213 put_bits(&s->pb, 16, 17);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
214 if(lossless && s->avctx->pix_fmt == PIX_FMT_RGB32)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
215 put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
216 else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
217 put_bits(&s->pb, 8, 8); /* 8 bits/component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
218 put_bits(&s->pb, 16, s->height);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
219 put_bits(&s->pb, 16, s->width);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
220 put_bits(&s->pb, 8, 3); /* 3 components */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
221
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
222 /* Y component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
223 put_bits(&s->pb, 8, 1); /* component number */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
224 put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
225 put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
226 put_bits(&s->pb, 8, 0); /* select matrix */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
227
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
228 /* Cb component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
229 put_bits(&s->pb, 8, 2); /* component number */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
230 put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
231 put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
232 #ifdef TWOMATRIXES
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
233 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
234 #else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
235 put_bits(&s->pb, 8, 0); /* select matrix */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
236 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
237
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
238 /* Cr component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
239 put_bits(&s->pb, 8, 3); /* component number */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
240 put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
241 put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
242 #ifdef TWOMATRIXES
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
243 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
244 #else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
245 put_bits(&s->pb, 8, 0); /* select matrix */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
246 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
247
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
248 /* scan header */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
249 put_marker(&s->pb, SOS);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
250 put_bits(&s->pb, 16, 12); /* length */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
251 put_bits(&s->pb, 8, 3); /* 3 components */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
252
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
253 /* Y component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
254 put_bits(&s->pb, 8, 1); /* index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
255 put_bits(&s->pb, 4, 0); /* DC huffman table index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
256 put_bits(&s->pb, 4, 0); /* AC huffman table index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
257
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
258 /* Cb component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
259 put_bits(&s->pb, 8, 2); /* index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
260 put_bits(&s->pb, 4, 1); /* DC huffman table index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
261 put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
262
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
263 /* Cr component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
264 put_bits(&s->pb, 8, 3); /* index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
265 put_bits(&s->pb, 4, 1); /* DC huffman table index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
266 put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
267
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
268 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
269
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
270 switch(s->avctx->codec_id){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
271 case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
272 case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
273 default: assert(0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
274 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
275
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
276 put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
277 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
278
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
279 static void escape_FF(MpegEncContext *s, int start)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
280 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
281 int size= put_bits_count(&s->pb) - start*8;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
282 int i, ff_count;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
283 uint8_t *buf= s->pb.buf + start;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
284 int align= (-(size_t)(buf))&3;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
285
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
286 assert((size&7) == 0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
287 size >>= 3;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
288
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
289 ff_count=0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
290 for(i=0; i<size && i<align; i++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
291 if(buf[i]==0xFF) ff_count++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
292 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
293 for(; i<size-15; i+=16){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
294 int acc, v;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
295
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
296 v= *(uint32_t*)(&buf[i]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
297 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
298 v= *(uint32_t*)(&buf[i+4]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
299 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
300 v= *(uint32_t*)(&buf[i+8]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
301 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
302 v= *(uint32_t*)(&buf[i+12]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
303 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
304
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
305 acc>>=4;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
306 acc+= (acc>>16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
307 acc+= (acc>>8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
308 ff_count+= acc&0xFF;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
309 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
310 for(; i<size; i++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
311 if(buf[i]==0xFF) ff_count++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
312 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
313
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
314 if(ff_count==0) return;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
315
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
316 /* skip put bits */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
317 for(i=0; i<ff_count-3; i+=4)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
318 put_bits(&s->pb, 32, 0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
319 put_bits(&s->pb, (ff_count-i)*8, 0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
320 flush_put_bits(&s->pb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
321
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
322 for(i=size-1; ff_count; i--){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
323 int v= buf[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
324
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
325 if(v==0xFF){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
326 //printf("%d %d\n", i, ff_count);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
327 buf[i+ff_count]= 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
328 ff_count--;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
329 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
330
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
331 buf[i+ff_count]= v;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
332 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
333 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
334
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
335 void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
336 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
337 int length;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
338 length= (-put_bits_count(pbc))&7;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
339 if(length) put_bits(pbc, length, (1<<length)-1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
340 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
341
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
342 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
343 {
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
344 ff_mjpeg_encode_stuffing(&s->pb);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
345 flush_put_bits(&s->pb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
346
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
347 assert((s->header_bits&7)==0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
348
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
349 escape_FF(s, s->header_bits>>3);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
350
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
351 put_marker(&s->pb, EOI);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
352 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
353
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
354 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
355 uint8_t *huff_size, uint16_t *huff_code)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
356 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
357 int mant, nbits;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
358
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
359 if (val == 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
360 put_bits(&s->pb, huff_size[0], huff_code[0]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
361 } else {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
362 mant = val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
363 if (val < 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
364 val = -val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
365 mant--;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
366 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
367
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
368 nbits= av_log2_16bit(val) + 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
369
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
370 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
371
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
372 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
373 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
374 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
375
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
376 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
377 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
378 int mant, nbits, code, i, j;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
379 int component, dc, run, last_index, val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
380 MJpegContext *m = s->mjpeg_ctx;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
381 uint8_t *huff_size_ac;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
382 uint16_t *huff_code_ac;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
383
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
384 /* DC coef */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
385 component = (n <= 3 ? 0 : (n&1) + 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
386 dc = block[0]; /* overflow is impossible */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
387 val = dc - s->last_dc[component];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
388 if (n < 4) {
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
389 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
390 huff_size_ac = m->huff_size_ac_luminance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
391 huff_code_ac = m->huff_code_ac_luminance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
392 } else {
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
393 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
394 huff_size_ac = m->huff_size_ac_chrominance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
395 huff_code_ac = m->huff_code_ac_chrominance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
396 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
397 s->last_dc[component] = dc;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
398
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
399 /* AC coefs */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
400
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
401 run = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
402 last_index = s->block_last_index[n];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
403 for(i=1;i<=last_index;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
404 j = s->intra_scantable.permutated[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
405 val = block[j];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
406 if (val == 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
407 run++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
408 } else {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
409 while (run >= 16) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
410 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
411 run -= 16;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
412 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
413 mant = val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
414 if (val < 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
415 val = -val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
416 mant--;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
417 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
418
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
419 nbits= av_log2(val) + 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
420 code = (run << 4) | nbits;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
421
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
422 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
423
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
424 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
425 run = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
426 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
427 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
428
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
429 /* output EOB only if not already 64 values */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
430 if (last_index < 63 || run != 0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
431 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
432 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
433
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
434 void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
435 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
436 int i;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
437 for(i=0;i<5;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
438 encode_block(s, block[i], i);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
439 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
440 if (s->chroma_format == CHROMA_420) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
441 encode_block(s, block[5], 5);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
442 } else {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
443 encode_block(s, block[6], 6);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
444 encode_block(s, block[5], 5);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
445 encode_block(s, block[7], 7);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
446 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
447 }
5030
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
448
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
449 AVCodec mjpeg_encoder = {
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
450 "mjpeg",
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
451 CODEC_TYPE_VIDEO,
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
452 CODEC_ID_MJPEG,
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
453 sizeof(MpegEncContext),
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
454 MPV_encode_init,
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
455 MPV_encode_picture,
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
456 MPV_encode_end,
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
457 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, -1},
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
458 };