annotate mjpegenc.c @ 11268:e817a3c2ec2e libavcodec

Replace /2 by faster >>1 as the mvd values are now all positive.
author michael
date Wed, 24 Feb 2010 01:57:31 +0000
parents 7127645ee791
children 8a4984c5cacc
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
8629
04423b2f6e0b cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents: 7260
diff changeset
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5020
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 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 8629
diff changeset
29 * @file libavcodec/mjpegenc.c
5020
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
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 5214
diff changeset
47 av_cold 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,
7136
7a73d76aaaa0 remove duplicate tables
stefang
parents: 7040
diff changeset
62 ff_mjpeg_val_dc);
5021
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,
7136
7a73d76aaaa0 remove duplicate tables
stefang
parents: 7040
diff changeset
66 ff_mjpeg_val_dc);
5021
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);
9431
932543edc1d2 Rename pbBufPtr() to put_bits_ptr().
stefano
parents: 8718
diff changeset
138 ptr = put_bits_ptr(p);
5020
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,
7136
7a73d76aaaa0 remove duplicate tables
stefang
parents: 7040
diff changeset
142 ff_mjpeg_val_dc);
5021
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,
7136
7a73d76aaaa0 remove duplicate tables
stefang
parents: 7040
diff changeset
144 ff_mjpeg_val_dc);
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);
9431
932543edc1d2 Rename pbBufPtr() to put_bits_ptr().
stefano
parents: 8718
diff changeset
177 ptr = put_bits_ptr(p);
5020
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);
9431
932543edc1d2 Rename pbBufPtr() to put_bits_ptr().
stefano
parents: 8718
diff changeset
189 ptr = put_bits_ptr(p);
5020
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);
10666
7127645ee791 Lossless jpeg expects and uses BGRA not RGB32 (this probably caused a problem on
michael
parents: 10327
diff changeset
214 if(lossless && s->avctx->pix_fmt == PIX_FMT_BGRA)
5020
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 flush_put_bits(&s->pb);
10327
bf04c3ecdfe5 Use skip_put_bytes in MJPEG encoder instead of filling all bytes with 0
reimar
parents: 10146
diff changeset
317 skip_put_bytes(&s->pb, ff_count);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
318
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
319 for(i=size-1; ff_count; i--){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
320 int v= buf[i];
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 if(v==0xFF){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
323 //printf("%d %d\n", i, ff_count);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
324 buf[i+ff_count]= 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
325 ff_count--;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
326 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
327
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
328 buf[i+ff_count]= v;
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
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
332 void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
5020
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 int length;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
335 length= (-put_bits_count(pbc))&7;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
336 if(length) put_bits(pbc, length, (1<<length)-1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
337 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
338
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
339 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
340 {
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
341 ff_mjpeg_encode_stuffing(&s->pb);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
342 flush_put_bits(&s->pb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
343
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
344 assert((s->header_bits&7)==0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
345
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
346 escape_FF(s, s->header_bits>>3);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
347
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
348 put_marker(&s->pb, EOI);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
349 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
350
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
351 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
352 uint8_t *huff_size, uint16_t *huff_code)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
353 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
354 int mant, nbits;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
355
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
356 if (val == 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
357 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
358 } else {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
359 mant = val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
360 if (val < 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
361 val = -val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
362 mant--;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
363 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
364
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
365 nbits= av_log2_16bit(val) + 1;
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 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
368
7260
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7136
diff changeset
369 put_sbits(&s->pb, nbits, mant);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
370 }
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
373 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
374 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
375 int mant, nbits, code, i, j;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
376 int component, dc, run, last_index, val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
377 MJpegContext *m = s->mjpeg_ctx;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
378 uint8_t *huff_size_ac;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
379 uint16_t *huff_code_ac;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
380
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
381 /* DC coef */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
382 component = (n <= 3 ? 0 : (n&1) + 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
383 dc = block[0]; /* overflow is impossible */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
384 val = dc - s->last_dc[component];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
385 if (n < 4) {
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
386 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
387 huff_size_ac = m->huff_size_ac_luminance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
388 huff_code_ac = m->huff_code_ac_luminance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
389 } else {
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
390 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
391 huff_size_ac = m->huff_size_ac_chrominance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
392 huff_code_ac = m->huff_code_ac_chrominance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
393 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
394 s->last_dc[component] = dc;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
395
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
396 /* AC coefs */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
397
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
398 run = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
399 last_index = s->block_last_index[n];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
400 for(i=1;i<=last_index;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
401 j = s->intra_scantable.permutated[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
402 val = block[j];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
403 if (val == 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
404 run++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
405 } else {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
406 while (run >= 16) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
407 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
408 run -= 16;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
409 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
410 mant = val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
411 if (val < 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
412 val = -val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
413 mant--;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
414 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
415
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
416 nbits= av_log2(val) + 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
417 code = (run << 4) | nbits;
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 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
420
7260
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7136
diff changeset
421 put_sbits(&s->pb, nbits, mant);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
422 run = 0;
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 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
425
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
426 /* output EOB only if not already 64 values */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
427 if (last_index < 63 || run != 0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
428 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
429 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
430
5029
dbaa06366c3c add a proper prefix to all mjpeg encoder exported functions
aurel
parents: 5028
diff changeset
431 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
432 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
433 int i;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
434 for(i=0;i<5;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
435 encode_block(s, block[i], i);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
436 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
437 if (s->chroma_format == CHROMA_420) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
438 encode_block(s, block[5], 5);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
439 } else {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
440 encode_block(s, block[6], 6);
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 encode_block(s, block[7], 7);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
443 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
444 }
5030
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
445
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
446 AVCodec mjpeg_encoder = {
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
447 "mjpeg",
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
448 CODEC_TYPE_VIDEO,
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
449 CODEC_ID_MJPEG,
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
450 sizeof(MpegEncContext),
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
451 MPV_encode_init,
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
452 MPV_encode_picture,
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
453 MPV_encode_end,
10146
38cfe222e1a4 Mark all pix_fmts and supported_framerates compound literals as const.
reimar
parents: 9431
diff changeset
454 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6788
diff changeset
455 .long_name= NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
5030
9ce0052d533a move the mjpeg_encoder struct from mpegvideo.c to mjpegenc.c
aurel
parents: 5029
diff changeset
456 };