annotate mjpegenc.c @ 5020:eb0ad6423405 libavcodec

split mjpeg.c into an encoder and a decoder file
author aurel
date Thu, 17 May 2007 16:29:11 +0000
parents
children d73237575709
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 *
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
7 * This file is part of FFmpeg.
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
8 *
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
9 * 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
10 * 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
11 * License as published by the Free Software Foundation; either
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
12 * 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
13 *
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
14 * 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
15 * 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
16 * 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
17 * Lesser General Public License for more details.
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
18 *
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
19 * 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
20 * 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
21 * 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
22 *
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
23 * Support for external huffman table, various fixes (AVID workaround),
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
24 * aspecting, new decode_frame mechanism and apple mjpeg-b support
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
25 * by Alex Beregszaszi
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"
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
40
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
41 /* 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
42 /* not yet working */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
43 #undef TWOMATRIXES
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
44
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
45 typedef struct MJpegContext {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
46 uint8_t huff_size_dc_luminance[12]; //FIXME use array [3] instead of lumi / chrom, for easier addressing
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
47 uint16_t huff_code_dc_luminance[12];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
48 uint8_t huff_size_dc_chrominance[12];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
49 uint16_t huff_code_dc_chrominance[12];
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 uint8_t huff_size_ac_luminance[256];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
52 uint16_t huff_code_ac_luminance[256];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
53 uint8_t huff_size_ac_chrominance[256];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
54 uint16_t huff_code_ac_chrominance[256];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
55 } MJpegContext;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
56
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 int mjpeg_init(MpegEncContext *s)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
59 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
60 MJpegContext *m;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
61
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
62 m = av_malloc(sizeof(MJpegContext));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
63 if (!m)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
64 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
65
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
66 s->min_qcoeff=-1023;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
67 s->max_qcoeff= 1023;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
68
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
69 /* build all the huffman tables */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
70 build_huffman_codes(m->huff_size_dc_luminance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
71 m->huff_code_dc_luminance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
72 bits_dc_luminance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
73 val_dc_luminance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
74 build_huffman_codes(m->huff_size_dc_chrominance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
75 m->huff_code_dc_chrominance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
76 bits_dc_chrominance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
77 val_dc_chrominance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
78 build_huffman_codes(m->huff_size_ac_luminance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
79 m->huff_code_ac_luminance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
80 bits_ac_luminance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
81 val_ac_luminance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
82 build_huffman_codes(m->huff_size_ac_chrominance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
83 m->huff_code_ac_chrominance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
84 bits_ac_chrominance,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
85 val_ac_chrominance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
86
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
87 s->mjpeg_ctx = m;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
88 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
89 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
90
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
91 void mjpeg_close(MpegEncContext *s)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
92 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
93 av_free(s->mjpeg_ctx);
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
96 /* table_class: 0 = DC coef, 1 = AC coefs */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
97 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
98 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
99 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
100 PutBitContext *p = &s->pb;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
101 int n, i;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
102
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
103 put_bits(p, 4, table_class);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
104 put_bits(p, 4, table_id);
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 n = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
107 for(i=1;i<=16;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
108 n += bits_table[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
109 put_bits(p, 8, bits_table[i]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
110 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
111
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
112 for(i=0;i<n;i++)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
113 put_bits(p, 8, value_table[i]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
114
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
115 return n + 17;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
116 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
117
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
118 static void jpeg_table_header(MpegEncContext *s)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
119 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
120 PutBitContext *p = &s->pb;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
121 int i, j, size;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
122 uint8_t *ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
123
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
124 /* quant matrixes */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
125 put_marker(p, DQT);
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, 16, 2 + 2 * (1 + 64));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
128 #else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
129 put_bits(p, 16, 2 + 1 * (1 + 64));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
130 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
131 put_bits(p, 4, 0); /* 8 bit precision */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
132 put_bits(p, 4, 0); /* table 0 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
133 for(i=0;i<64;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
134 j = s->intra_scantable.permutated[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
135 put_bits(p, 8, s->intra_matrix[j]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
136 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
137 #ifdef TWOMATRIXES
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
138 put_bits(p, 4, 0); /* 8 bit precision */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
139 put_bits(p, 4, 1); /* table 1 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
140 for(i=0;i<64;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
141 j = s->intra_scantable.permutated[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
142 put_bits(p, 8, s->chroma_intra_matrix[j]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
143 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
144 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
145
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
146 /* huffman table */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
147 put_marker(p, DHT);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
148 flush_put_bits(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
149 ptr = pbBufPtr(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
150 put_bits(p, 16, 0); /* patched later */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
151 size = 2;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
152 size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
153 size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
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 size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
156 size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
157 ptr[0] = size >> 8;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
158 ptr[1] = size;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
159 }
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 static void jpeg_put_comments(MpegEncContext *s)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
162 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
163 PutBitContext *p = &s->pb;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
164 int size;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
165 uint8_t *ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
166
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
167 if (s->aspect_ratio_info /* && !lossless */)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
168 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
169 /* JFIF header */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
170 put_marker(p, APP0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
171 put_bits(p, 16, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
172 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
173 put_bits(p, 16, 0x0201); /* v 1.02 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
174 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
175 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
176 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
177 put_bits(p, 8, 0); /* thumbnail width */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
178 put_bits(p, 8, 0); /* thumbnail height */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
179 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
180
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
181 /* comment */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
182 if(!(s->flags & CODEC_FLAG_BITEXACT)){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
183 put_marker(p, COM);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
184 flush_put_bits(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
185 ptr = pbBufPtr(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
186 put_bits(p, 16, 0); /* patched later */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
187 ff_put_string(p, LIBAVCODEC_IDENT, 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
188 size = strlen(LIBAVCODEC_IDENT)+3;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
189 ptr[0] = size >> 8;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
190 ptr[1] = size;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
191 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
192
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
193 if( s->avctx->pix_fmt == PIX_FMT_YUV420P
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
194 ||s->avctx->pix_fmt == PIX_FMT_YUV422P
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
195 ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
196 put_marker(p, COM);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
197 flush_put_bits(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
198 ptr = pbBufPtr(p);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
199 put_bits(p, 16, 0); /* patched later */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
200 ff_put_string(p, "CS=ITU601", 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
201 size = strlen("CS=ITU601")+3;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
202 ptr[0] = size >> 8;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
203 ptr[1] = size;
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 }
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 void mjpeg_picture_header(MpegEncContext *s)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
208 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
209 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
210
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
211 put_marker(&s->pb, SOI);
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 jpeg_put_comments(s);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
214
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
215 jpeg_table_header(s);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
216
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
217 switch(s->avctx->codec_id){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
218 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
219 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
220 default: assert(0);
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
223 put_bits(&s->pb, 16, 17);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
224 if(lossless && s->avctx->pix_fmt == PIX_FMT_RGB32)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
225 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
226 else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
227 put_bits(&s->pb, 8, 8); /* 8 bits/component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
228 put_bits(&s->pb, 16, s->height);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
229 put_bits(&s->pb, 16, s->width);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
230 put_bits(&s->pb, 8, 3); /* 3 components */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
231
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
232 /* Y component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
233 put_bits(&s->pb, 8, 1); /* component number */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
234 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
235 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
236 put_bits(&s->pb, 8, 0); /* select matrix */
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 /* Cb component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
239 put_bits(&s->pb, 8, 2); /* 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[1]); /* 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[1]); /* 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 /* Cr component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
249 put_bits(&s->pb, 8, 3); /* component number */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
250 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
251 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
252 #ifdef TWOMATRIXES
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
253 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
254 #else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
255 put_bits(&s->pb, 8, 0); /* select matrix */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
256 #endif
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 /* scan header */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
259 put_marker(&s->pb, SOS);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
260 put_bits(&s->pb, 16, 12); /* length */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
261 put_bits(&s->pb, 8, 3); /* 3 components */
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 /* Y component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
264 put_bits(&s->pb, 8, 1); /* index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
265 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
266 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
267
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
268 /* Cb component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
269 put_bits(&s->pb, 8, 2); /* index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
270 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
271 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
272
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
273 /* Cr component */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
274 put_bits(&s->pb, 8, 3); /* index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
275 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
276 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
277
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
278 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
279
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
280 switch(s->avctx->codec_id){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
281 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
282 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
283 default: assert(0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
284 }
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 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
287 }
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 static void escape_FF(MpegEncContext *s, int start)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
290 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
291 int size= put_bits_count(&s->pb) - start*8;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
292 int i, ff_count;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
293 uint8_t *buf= s->pb.buf + start;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
294 int align= (-(size_t)(buf))&3;
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 assert((size&7) == 0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
297 size >>= 3;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
298
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
299 ff_count=0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
300 for(i=0; i<size && i<align; i++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
301 if(buf[i]==0xFF) ff_count++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
302 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
303 for(; i<size-15; i+=16){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
304 int acc, v;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
305
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
306 v= *(uint32_t*)(&buf[i]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
307 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
308 v= *(uint32_t*)(&buf[i+4]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
309 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
310 v= *(uint32_t*)(&buf[i+8]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
311 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
312 v= *(uint32_t*)(&buf[i+12]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
313 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
314
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
315 acc>>=4;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
316 acc+= (acc>>16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
317 acc+= (acc>>8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
318 ff_count+= acc&0xFF;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
319 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
320 for(; i<size; i++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
321 if(buf[i]==0xFF) ff_count++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
322 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
323
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
324 if(ff_count==0) return;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
325
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
326 /* skip put bits */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
327 for(i=0; i<ff_count-3; i+=4)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
328 put_bits(&s->pb, 32, 0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
329 put_bits(&s->pb, (ff_count-i)*8, 0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
330 flush_put_bits(&s->pb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
331
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
332 for(i=size-1; ff_count; i--){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
333 int v= buf[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
334
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
335 if(v==0xFF){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
336 //printf("%d %d\n", i, ff_count);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
337 buf[i+ff_count]= 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
338 ff_count--;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
339 }
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 buf[i+ff_count]= v;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
342 }
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
345 void ff_mjpeg_stuffing(PutBitContext * pbc)
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 int length;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
348 length= (-put_bits_count(pbc))&7;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
349 if(length) put_bits(pbc, length, (1<<length)-1);
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
352 void mjpeg_picture_trailer(MpegEncContext *s)
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 ff_mjpeg_stuffing(&s->pb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
355 flush_put_bits(&s->pb);
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 assert((s->header_bits&7)==0);
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 escape_FF(s, s->header_bits>>3);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
360
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
361 put_marker(&s->pb, EOI);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
362 }
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 static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
365 uint8_t *huff_size, uint16_t *huff_code)
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 int mant, nbits;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
368
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
369 if (val == 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
370 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
371 } else {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
372 mant = val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
373 if (val < 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
374 val = -val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
375 mant--;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
376 }
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 nbits= av_log2_16bit(val) + 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
379
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
380 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
381
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
382 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
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 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
385
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
386 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
387 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
388 int mant, nbits, code, i, j;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
389 int component, dc, run, last_index, val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
390 MJpegContext *m = s->mjpeg_ctx;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
391 uint8_t *huff_size_ac;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
392 uint16_t *huff_code_ac;
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 /* DC coef */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
395 component = (n <= 3 ? 0 : (n&1) + 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
396 dc = block[0]; /* overflow is impossible */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
397 val = dc - s->last_dc[component];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
398 if (n < 4) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
399 mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
400 huff_size_ac = m->huff_size_ac_luminance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
401 huff_code_ac = m->huff_code_ac_luminance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
402 } else {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
403 mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
404 huff_size_ac = m->huff_size_ac_chrominance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
405 huff_code_ac = m->huff_code_ac_chrominance;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
406 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
407 s->last_dc[component] = dc;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
408
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
409 /* AC coefs */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
410
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
411 run = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
412 last_index = s->block_last_index[n];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
413 for(i=1;i<=last_index;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
414 j = s->intra_scantable.permutated[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
415 val = block[j];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
416 if (val == 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
417 run++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
418 } else {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
419 while (run >= 16) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
420 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
421 run -= 16;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
422 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
423 mant = val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
424 if (val < 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
425 val = -val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
426 mant--;
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 nbits= av_log2(val) + 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
430 code = (run << 4) | nbits;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
431
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
432 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
433
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
434 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
435 run = 0;
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 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
438
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
439 /* output EOB only if not already 64 values */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
440 if (last_index < 63 || run != 0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
441 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
442 }
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 void mjpeg_encode_mb(MpegEncContext *s,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
445 DCTELEM block[6][64])
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 int i;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
448 for(i=0;i<5;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
449 encode_block(s, block[i], i);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
450 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
451 if (s->chroma_format == CHROMA_420) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
452 encode_block(s, block[5], 5);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
453 } else {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
454 encode_block(s, block[6], 6);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
455 encode_block(s, block[5], 5);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
456 encode_block(s, block[7], 7);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
457 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
458 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
459
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
460 static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
461 MpegEncContext * const s = avctx->priv_data;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
462 MJpegContext * const m = s->mjpeg_ctx;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
463 AVFrame *pict = data;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
464 const int width= s->width;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
465 const int height= s->height;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
466 AVFrame * const p= (AVFrame*)&s->current_picture;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
467 const int predictor= avctx->prediction_method+1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
468
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
469 init_put_bits(&s->pb, buf, buf_size);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
470
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
471 *p = *pict;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
472 p->pict_type= FF_I_TYPE;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
473 p->key_frame= 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
474
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
475 mjpeg_picture_header(s);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
476
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
477 s->header_bits= put_bits_count(&s->pb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
478
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
479 if(avctx->pix_fmt == PIX_FMT_RGB32){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
480 int x, y, i;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
481 const int linesize= p->linesize[0];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
482 uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
483 int left[3], top[3], topleft[3];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
484
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
485 for(i=0; i<3; i++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
486 buffer[0][i]= 1 << (9 - 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
487 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
488
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
489 for(y = 0; y < height; y++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
490 const int modified_predictor= y ? predictor : 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
491 uint8_t *ptr = p->data[0] + (linesize * y);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
492
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
493 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
494 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
495 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
496 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
497
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
498 for(i=0; i<3; i++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
499 top[i]= left[i]= topleft[i]= buffer[0][i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
500 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
501 for(x = 0; x < width; x++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
502 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
503 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
504 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
505
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
506 for(i=0;i<3;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
507 int pred, diff;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
508
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
509 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
510
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
511 topleft[i]= top[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
512 top[i]= buffer[x+1][i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
513
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
514 left[i]= buffer[x][i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
515
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
516 diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
517
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
518 if(i==0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
519 mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
520 else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
521 mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
522 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
523 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
524 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
525 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
526 int mb_x, mb_y, i;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
527 const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
528 const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
529
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
530 for(mb_y = 0; mb_y < mb_height; mb_y++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
531 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < mb_width * 4 * 3 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0]){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
532 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
533 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
534 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
535 for(mb_x = 0; mb_x < mb_width; mb_x++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
536 if(mb_x==0 || mb_y==0){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
537 for(i=0;i<3;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
538 uint8_t *ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
539 int x, y, h, v, linesize;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
540 h = s->mjpeg_hsample[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
541 v = s->mjpeg_vsample[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
542 linesize= p->linesize[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
543
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
544 for(y=0; y<v; y++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
545 for(x=0; x<h; x++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
546 int pred;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
547
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
548 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
549 if(y==0 && mb_y==0){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
550 if(x==0 && mb_x==0){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
551 pred= 128;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
552 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
553 pred= ptr[-1];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
554 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
555 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
556 if(x==0 && mb_x==0){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
557 pred= ptr[-linesize];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
558 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
559 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
560 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
561 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
562
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
563 if(i==0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
564 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
565 else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
566 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
567 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
568 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
569 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
570 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
571 for(i=0;i<3;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
572 uint8_t *ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
573 int x, y, h, v, linesize;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
574 h = s->mjpeg_hsample[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
575 v = s->mjpeg_vsample[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
576 linesize= p->linesize[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
577
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
578 for(y=0; y<v; y++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
579 for(x=0; x<h; x++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
580 int pred;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
581
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
582 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
583 //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
584 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
585
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
586 if(i==0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
587 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
588 else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
589 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
590 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
591 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
592 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
593 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
594 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
595 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
596 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
597
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
598 emms_c();
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
599
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
600 mjpeg_picture_trailer(s);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
601 s->picture_number++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
602
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
603 flush_put_bits(&s->pb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
604 return pbBufPtr(&s->pb) - s->pb.buf;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
605 // return (put_bits_count(&f->pb)+7)/8;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
606 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
607
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
608
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
609 AVCodec ljpeg_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
610 "ljpeg",
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
611 CODEC_TYPE_VIDEO,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
612 CODEC_ID_LJPEG,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
613 sizeof(MpegEncContext),
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
614 MPV_encode_init,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
615 encode_picture_lossless,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
616 MPV_encode_end,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
617 };