annotate mjpegdec.c @ 5054:6e7e23b6615d libavcodec

Add explanatory comments to some #endifs.
author diego
date Mon, 21 May 2007 07:01:42 +0000
parents 924fdd6175b1
children 93e369c55c31
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 decoder
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 mjpegdec.c
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
30 * MJPEG decoder.
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 "mjpeg.h"
5041
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
39 #include "mjpegdec.h"
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
40 #include "jpeglsdec.h"
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
43 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
44 int nb_codes, int use_static, int is_ac)
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 uint8_t huff_size[256+16];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
47 uint16_t huff_code[256+16];
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 assert(nb_codes <= 256);
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 memset(huff_size, 0, sizeof(huff_size));
5021
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
52 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
53
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
54 if(is_ac){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
55 memmove(huff_size+16, huff_size, sizeof(uint8_t)*nb_codes);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
56 memmove(huff_code+16, huff_code, sizeof(uint16_t)*nb_codes);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
57 memset(huff_size, 0, sizeof(uint8_t)*16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
58 memset(huff_code, 0, sizeof(uint16_t)*16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
59 nb_codes += 16;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
60 }
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 return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
63 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
64
5041
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
65 int ff_mjpeg_decode_init(AVCodecContext *avctx)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
66 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
67 MJpegDecodeContext *s = avctx->priv_data;
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 s->avctx = avctx;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
70 dsputil_init(&s->dsp, avctx);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
71 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
72 s->buffer_size = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
73 s->buffer = NULL;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
74 s->start_code = -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
75 s->first_picture = 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
76 s->org_height = avctx->coded_height;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
77
5021
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
78 build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
79 ff_mjpeg_val_dc_luminance, 12, 0, 0);
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
80 build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
81 ff_mjpeg_val_dc_chrominance, 12, 0, 0);
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
82 build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
83 ff_mjpeg_val_ac_luminance, 251, 0, 1);
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
84 build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance,
d73237575709 add proper prefix to new mjpeg extern func/var
aurel
parents: 5020
diff changeset
85 ff_mjpeg_val_ac_chrominance, 251, 0, 1);
5020
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 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
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 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
90 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
91 ff_mjpeg_decode_dht(s);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
92 /* should check for error - but dunno */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
93 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
94 if (avctx->extradata_size > 9 &&
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
95 AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
96 if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
97 s->interlace_polarity = 1; /* bottom field first */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
98 av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
102 return 0;
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
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 /* quantize tables */
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
107 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
5020
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 int len, index, i, j;
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 len = get_bits(&s->gb, 16) - 2;
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 while (len >= 65) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
114 /* only 8 bit precision handled */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
115 if (get_bits(&s->gb, 4) != 0)
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 av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
118 return -1;
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 index = get_bits(&s->gb, 4);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
121 if (index >= 4)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
122 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
123 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
124 /* read quant table */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
125 for(i=0;i<64;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
126 j = s->scantable.permutated[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
127 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
128 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
129
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
130 //XXX FIXME finetune, and perhaps add dc too
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
131 s->qscale[index]= FFMAX(
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
132 s->quant_matrixes[index][s->scantable.permutated[1]],
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
133 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
134 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
135 len -= 65;
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
138 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
139 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
140
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
141 /* decode huffman tables and build VLC decoders */
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
142 int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
5020
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 int len, index, i, class, n, v, code_max;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
145 uint8_t bits_table[17];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
146 uint8_t val_table[256];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
147
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
148 len = get_bits(&s->gb, 16) - 2;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
149
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
150 while (len > 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
151 if (len < 17)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
152 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
153 class = get_bits(&s->gb, 4);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
154 if (class >= 2)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
155 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
156 index = get_bits(&s->gb, 4);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
157 if (index >= 4)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
158 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
159 n = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
160 for(i=1;i<=16;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
161 bits_table[i] = get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
162 n += bits_table[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
163 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
164 len -= 17;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
165 if (len < n || n > 256)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
166 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
167
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
168 code_max = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
169 for(i=0;i<n;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
170 v = get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
171 if (v > code_max)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
172 code_max = v;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
173 val_table[i] = v;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
174 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
175 len -= n;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
176
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
177 /* build VLC and flush previous vlc if present */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
178 free_vlc(&s->vlcs[class][index]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
179 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
180 class, index, code_max + 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
181 if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
182 return -1;
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 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
185 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
186 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
187
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
188 int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
189 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
190 int len, nb_components, i, width, height, pix_fmt_id;
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 /* XXX: verify len field validity */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
193 len = get_bits(&s->gb, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
194 s->bits= get_bits(&s->gb, 8);
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 if(s->pegasus_rct) s->bits=9;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
197 if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly
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 if (s->bits != 8 && !s->lossless){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
200 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
201 return -1;
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
204 height = get_bits(&s->gb, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
205 width = get_bits(&s->gb, 16);
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 //HACK for odd_height.mov
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
208 if(s->interlaced && s->width == width && s->height == height + 1)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
209 height= s->height;
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 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
212 if(avcodec_check_dimensions(s->avctx, width, height))
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
213 return -1;
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 nb_components = get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
216 if (nb_components <= 0 ||
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
217 nb_components > MAX_COMPONENTS)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
218 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
219 if (s->ls && !(s->bits <= 8 || nb_components == 1)){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
220 av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
221 return -1;
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 s->nb_components = nb_components;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
224 s->h_max = 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
225 s->v_max = 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
226 for(i=0;i<nb_components;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
227 /* component id */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
228 s->component_id[i] = get_bits(&s->gb, 8) - 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
229 s->h_count[i] = get_bits(&s->gb, 4);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
230 s->v_count[i] = get_bits(&s->gb, 4);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
231 /* compute hmax and vmax (only used in interleaved case) */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
232 if (s->h_count[i] > s->h_max)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
233 s->h_max = s->h_count[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
234 if (s->v_count[i] > s->v_max)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
235 s->v_max = s->v_count[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
236 s->quant_index[i] = get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
237 if (s->quant_index[i] >= 4)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
238 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
239 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
240 s->v_count[i], s->component_id[i], s->quant_index[i]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
241 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
242
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
243 if(s->ls && (s->h_max > 1 || s->v_max > 1)) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
244 av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
245 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
246 }
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 if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
249
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
250 /* if different size, realloc/alloc picture */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
251 /* XXX: also check h_count and v_count */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
252 if (width != s->width || height != s->height) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
253 av_freep(&s->qscale_table);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
254
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
255 s->width = width;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
256 s->height = height;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
257 s->interlaced = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
258
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
259 /* test interlaced mode */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
260 if (s->first_picture &&
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
261 s->org_height != 0 &&
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
262 s->height < ((s->org_height * 3) / 4)) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
263 s->interlaced = 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
264 s->bottom_field = s->interlace_polarity;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
265 s->picture.interlaced_frame = 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
266 s->picture.top_field_first = !s->interlace_polarity;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
267 height *= 2;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
268 }
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 avcodec_set_dimensions(s->avctx, width, height);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
271
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
272 s->qscale_table= av_mallocz((s->width+15)/16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
273
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
274 s->first_picture = 0;
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
277 if(s->interlaced && (s->bottom_field == !s->interlace_polarity))
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
278 return 0;
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 /* XXX: not complete test ! */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
281 pix_fmt_id = (s->h_count[0] << 20) | (s->v_count[0] << 16) |
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
282 (s->h_count[1] << 12) | (s->v_count[1] << 8) |
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
283 (s->h_count[2] << 4) | s->v_count[2];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
284 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
285 switch(pix_fmt_id){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
286 case 0x222222:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
287 case 0x111111:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
288 if(s->rgb){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
289 s->avctx->pix_fmt = PIX_FMT_RGB32;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
290 }else if(s->nb_components==3)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
291 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
292 else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
293 s->avctx->pix_fmt = PIX_FMT_GRAY8;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
294 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
295 case 0x211111:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
296 case 0x221212:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
297 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
298 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
299 default:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
300 case 0x221111:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
301 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
302 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
303 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
304 if(s->ls){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
305 if(s->nb_components > 1)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
306 s->avctx->pix_fmt = PIX_FMT_RGB24;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
307 else if(s->bits <= 8)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
308 s->avctx->pix_fmt = PIX_FMT_GRAY8;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
309 else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
310 s->avctx->pix_fmt = PIX_FMT_GRAY16;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
311 }
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 if(s->picture.data[0])
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
314 s->avctx->release_buffer(s->avctx, &s->picture);
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 s->picture.reference= 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
317 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
318 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
319 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
320 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
321 s->picture.pict_type= I_TYPE;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
322 s->picture.key_frame= 1;
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 for(i=0; i<3; i++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
325 s->linesize[i]= s->picture.linesize[i] << s->interlaced;
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 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
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 if (len != (8+(3*nb_components)))
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 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
335 /* totally blank picture as progressive JPEG will only add details to it */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
336 if(s->progressive){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
337 memset(s->picture.data[0], 0, s->picture.linesize[0] * s->height);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
338 memset(s->picture.data[1], 0, s->picture.linesize[1] * s->height >> (s->v_max - s->v_count[1]));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
339 memset(s->picture.data[2], 0, s->picture.linesize[2] * s->height >> (s->v_max - s->v_count[2]));
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 return 0;
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 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
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 int code;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
347 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
348 if (code < 0)
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 av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
351 &s->vlcs[0][dc_index]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
352 return 0xffff;
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
355 if(code)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
356 return get_xbits(&s->gb, code);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
357 else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
358 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
359 }
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 /* decode block and dequantize */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
362 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
363 int component, int dc_index, int ac_index, int16_t *quant_matrix)
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 int code, i, j, level, val;
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 /* DC coef */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
368 val = mjpeg_decode_dc(s, dc_index);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
369 if (val == 0xffff) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
370 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
371 return -1;
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 val = val * quant_matrix[0] + s->last_dc[component];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
374 s->last_dc[component] = val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
375 block[0] = val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
376 /* AC coefs */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
377 i = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
378 {OPEN_READER(re, &s->gb)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
379 for(;;) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
380 UPDATE_CACHE(re, &s->gb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
381 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
382
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
383 /* EOB */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
384 if (code == 0x10)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
385 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
386 i += ((unsigned)code) >> 4;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
387 if(code != 0x100){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
388 code &= 0xf;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
389 if(code > MIN_CACHE_BITS - 16){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
390 UPDATE_CACHE(re, &s->gb)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
391 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
392 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
393 int cache=GET_CACHE(re,&s->gb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
394 int sign=(~cache)>>31;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
395 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
396 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
397
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
398 LAST_SKIP_BITS(re, &s->gb, code)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
399
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
400 if (i >= 63) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
401 if(i == 63){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
402 j = s->scantable.permutated[63];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
403 block[j] = level * quant_matrix[j];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
404 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
405 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
406 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
407 return -1;
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 j = s->scantable.permutated[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
410 block[j] = level * quant_matrix[j];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
411 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
412 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
413 CLOSE_READER(re, &s->gb)}
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 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
416 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
417
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
418 /* decode block and dequantize - progressive JPEG version */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
419 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
420 int component, int dc_index, int ac_index, int16_t *quant_matrix,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
421 int ss, int se, int Ah, int Al, int *EOBRUN)
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 int code, i, j, level, val, run;
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 /* DC coef */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
426 if(!ss){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
427 val = mjpeg_decode_dc(s, dc_index);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
428 if (val == 0xffff) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
429 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
430 return -1;
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 val = (val * quant_matrix[0] << Al) + s->last_dc[component];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
433 }else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
434 val = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
435 s->last_dc[component] = val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
436 block[0] = val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
437 if(!se) return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
438 /* AC coefs */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
439 if(*EOBRUN){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
440 (*EOBRUN)--;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
441 return 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 {OPEN_READER(re, &s->gb)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
444 for(i=ss;;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
445 UPDATE_CACHE(re, &s->gb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
446 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
447 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
448 code -= 16;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
449 if(code & 0xF) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
450 i += ((unsigned) code) >> 4;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
451 code &= 0xf;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
452 if(code > MIN_CACHE_BITS - 16){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
453 UPDATE_CACHE(re, &s->gb)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
454 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
455 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
456 int cache=GET_CACHE(re,&s->gb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
457 int sign=(~cache)>>31;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
458 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
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
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
461 LAST_SKIP_BITS(re, &s->gb, code)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
462
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
463 if (i >= se) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
464 if(i == se){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
465 j = s->scantable.permutated[se];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
466 block[j] = level * quant_matrix[j] << Al;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
467 break;
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 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
470 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
471 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
472 j = s->scantable.permutated[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
473 block[j] = level * quant_matrix[j] << Al;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
474 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
475 run = ((unsigned) code) >> 4;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
476 if(run == 0xF){// ZRL - skip 15 coefficients
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
477 i += 15;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
478 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
479 val = run;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
480 run = (1 << run);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
481 UPDATE_CACHE(re, &s->gb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
482 run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
483 if(val)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
484 LAST_SKIP_BITS(re, &s->gb, val);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
485 *EOBRUN = run - 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
486 break;
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 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
490 CLOSE_READER(re, &s->gb)}
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
491
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
492 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
493 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
494
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
495 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
496 int i, mb_x, mb_y;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
497 uint16_t buffer[32768][4];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
498 int left[3], top[3], topleft[3];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
499 const int linesize= s->linesize[0];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
500 const int mask= (1<<s->bits)-1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
501
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
502 if((unsigned)s->mb_width > 32768) //dynamic alloc
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
503 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
504
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
505 for(i=0; i<3; i++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
506 buffer[0][i]= 1 << (s->bits + point_transform - 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
507 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
508 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
509 const int modified_predictor= mb_y ? predictor : 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
510 uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
511
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
512 if (s->interlaced && s->bottom_field)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
513 ptr += linesize >> 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
514
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
515 for(i=0; i<3; i++){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
516 top[i]= left[i]= topleft[i]= buffer[0][i];
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 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
519 if (s->restart_interval && !s->restart_count)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
520 s->restart_count = s->restart_interval;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
521
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
522 for(i=0;i<3;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
523 int pred;
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 topleft[i]= top[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
526 top[i]= buffer[mb_x][i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
527
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
528 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
529
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
530 left[i]=
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
531 buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
532 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
533
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
534 if (s->restart_interval && !--s->restart_count) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
535 align_get_bits(&s->gb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
536 skip_bits(&s->gb, 16); /* skip RSTn */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
537 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
538 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
539
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
540 if(s->rct){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
541 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
542 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
543 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
544 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
545 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
546 }else if(s->pegasus_rct){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
547 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
548 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
549 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
550 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
551 }
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 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
554 ptr[4*mb_x+0] = buffer[mb_x][0];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
555 ptr[4*mb_x+1] = buffer[mb_x][1];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
556 ptr[4*mb_x+2] = buffer[mb_x][2];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
557 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
558 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
559 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
560 return 0;
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 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
564 int i, mb_x, mb_y;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
565 const int nb_components=3;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
566
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
567 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
568 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
569 if (s->restart_interval && !s->restart_count)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
570 s->restart_count = s->restart_interval;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
571
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
572 if(mb_x==0 || mb_y==0 || s->interlaced){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
573 for(i=0;i<nb_components;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
574 uint8_t *ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
575 int n, h, v, x, y, c, j, linesize;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
576 n = s->nb_blocks[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
577 c = s->comp_index[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
578 h = s->h_scount[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
579 v = s->v_scount[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
580 x = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
581 y = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
582 linesize= s->linesize[c];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
583
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
584 for(j=0; j<n; j++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
585 int pred;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
586
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
587 ptr = s->picture.data[c] + (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
588 if(y==0 && mb_y==0){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
589 if(x==0 && mb_x==0){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
590 pred= 128 << point_transform;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
591 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
592 pred= ptr[-1];
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 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
595 if(x==0 && mb_x==0){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
596 pred= ptr[-linesize];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
597 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
598 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
599 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
600 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
601
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
602 if (s->interlaced && s->bottom_field)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
603 ptr += linesize >> 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
604 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
605
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
606 if (++x == h) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
607 x = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
608 y++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
609 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
610 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
611 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
612 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
613 for(i=0;i<nb_components;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
614 uint8_t *ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
615 int n, h, v, x, y, c, j, linesize;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
616 n = s->nb_blocks[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
617 c = s->comp_index[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
618 h = s->h_scount[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
619 v = s->v_scount[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
620 x = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
621 y = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
622 linesize= s->linesize[c];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
623
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
624 for(j=0; j<n; j++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
625 int pred;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
626
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
627 ptr = s->picture.data[c] + (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
628 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
629 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
630 if (++x == h) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
631 x = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
632 y++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
633 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
634 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
635 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
636 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
637 if (s->restart_interval && !--s->restart_count) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
638 align_get_bits(&s->gb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
639 skip_bits(&s->gb, 16); /* skip RSTn */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
640 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
641 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
642 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
643 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
644 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
645
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
646 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int ss, int se, int Ah, int Al){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
647 int i, mb_x, mb_y;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
648 int EOBRUN = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
649
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
650 if(Ah) return 0; /* TODO decode refinement planes too */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
651 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
652 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
653 if (s->restart_interval && !s->restart_count)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
654 s->restart_count = s->restart_interval;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
655
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
656 for(i=0;i<nb_components;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
657 uint8_t *ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
658 int n, h, v, x, y, c, j;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
659 n = s->nb_blocks[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
660 c = s->comp_index[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
661 h = s->h_scount[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
662 v = s->v_scount[i];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
663 x = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
664 y = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
665 for(j=0;j<n;j++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
666 memset(s->block, 0, sizeof(s->block));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
667 if (!s->progressive && decode_block(s, s->block, i,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
668 s->dc_index[i], s->ac_index[i],
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
669 s->quant_matrixes[ s->quant_index[c] ]) < 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
670 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
671 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
672 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
673 if (s->progressive && decode_block_progressive(s, s->block, i,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
674 s->dc_index[i], s->ac_index[i],
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
675 s->quant_matrixes[ s->quant_index[c] ], ss, se, Ah, Al, &EOBRUN) < 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
676 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
677 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
678 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
679 // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
680 ptr = s->picture.data[c] +
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
681 (((s->linesize[c] * (v * mb_y + y) * 8) +
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
682 (h * mb_x + x) * 8) >> s->avctx->lowres);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
683 if (s->interlaced && s->bottom_field)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
684 ptr += s->linesize[c] >> 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
685 //av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d %d %d %d %d \n", mb_x, mb_y, x, y, c, s->bottom_field, (v * mb_y + y) * 8, (h * mb_x + x) * 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
686 if(!s->progressive)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
687 s->dsp.idct_put(ptr, s->linesize[c], s->block);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
688 else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
689 s->dsp.idct_add(ptr, s->linesize[c], s->block);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
690 if (++x == h) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
691 x = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
692 y++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
693 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
694 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
695 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
696 /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
697 if (s->restart_interval && (s->restart_interval < 1350) &&
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
698 !--s->restart_count) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
699 align_get_bits(&s->gb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
700 skip_bits(&s->gb, 16); /* skip RSTn */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
701 for (i=0; i<nb_components; i++) /* reset dc */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
702 s->last_dc[i] = 1024;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
703 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
704 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
705 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
706 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
707 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
708
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
709 int ff_mjpeg_decode_sos(MJpegDecodeContext *s)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
710 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
711 int len, nb_components, i, h, v, predictor, point_transform;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
712 int vmax, hmax, index, id;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
713 const int block_size= s->lossless ? 1 : 8;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
714 int ilv, prev_shift;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
715
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
716 /* XXX: verify len field validity */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
717 len = get_bits(&s->gb, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
718 nb_components = get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
719 if (len != 6+2*nb_components)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
720 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
721 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
722 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
723 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
724 vmax = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
725 hmax = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
726 for(i=0;i<nb_components;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
727 id = get_bits(&s->gb, 8) - 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
728 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
729 /* find component index */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
730 for(index=0;index<s->nb_components;index++)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
731 if (id == s->component_id[index])
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
732 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
733 if (index == s->nb_components)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
734 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
735 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
736 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
737 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
738
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
739 s->comp_index[i] = index;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
740
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
741 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
742 s->h_scount[i] = s->h_count[index];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
743 s->v_scount[i] = s->v_count[index];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
744
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
745 s->dc_index[i] = get_bits(&s->gb, 4);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
746 s->ac_index[i] = get_bits(&s->gb, 4);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
747
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
748 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
749 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
750 goto out_of_range;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
751 #if 0 //buggy
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
752 switch(s->start_code)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
753 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
754 case SOF0:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
755 if (dc_index[i] > 1 || ac_index[i] > 1)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
756 goto out_of_range;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
757 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
758 case SOF1:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
759 case SOF2:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
760 if (dc_index[i] > 3 || ac_index[i] > 3)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
761 goto out_of_range;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
762 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
763 case SOF3:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
764 if (dc_index[i] > 3 || ac_index[i] != 0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
765 goto out_of_range;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
766 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
767 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
768 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
769 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
770
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
771 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
772 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
773 prev_shift = get_bits(&s->gb, 4); /* Ah */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
774 point_transform= get_bits(&s->gb, 4); /* Al */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
775
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
776 for(i=0;i<nb_components;i++)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
777 s->last_dc[i] = 1024;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
778
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
779 if (nb_components > 1) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
780 /* interleaved stream */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
781 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
782 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
783 } else if(!s->ls) { /* skip this for JPEG-LS */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
784 h = s->h_max / s->h_scount[0];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
785 v = s->v_max / s->v_scount[0];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
786 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
787 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
788 s->nb_blocks[0] = 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
789 s->h_scount[0] = 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
790 s->v_scount[0] = 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
791 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
792
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
793 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
794 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n", s->lossless ? "lossless" : "sequencial DCT", s->rgb ? "RGB" : "",
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
795 predictor, point_transform, ilv, s->bits,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
796 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
797
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
798
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
799 /* mjpeg-b can have padding bytes between sos and image data, skip them */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
800 for (i = s->mjpb_skiptosod; i > 0; i--)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
801 skip_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
802
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
803 if(s->lossless){
5041
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
804 if(ENABLE_JPEGLS_DECODER && s->ls){
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
805 // for(){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
806 // reset_ls_coding_parameters(s, 0);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
807
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
808 ff_jpegls_decode_picture(s, predictor, point_transform, ilv);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
809 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
810 if(s->rgb){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
811 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
812 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
813 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
814 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
815 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
816 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
817 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
818 }else{
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
819 if(mjpeg_decode_scan(s, nb_components, predictor, ilv, prev_shift, point_transform) < 0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
820 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
821 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
822 emms_c();
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
823 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
824 out_of_range:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
825 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
826 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
827 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
828
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
829 static int mjpeg_decode_dri(MJpegDecodeContext *s)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
830 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
831 if (get_bits(&s->gb, 16) != 4)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
832 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
833 s->restart_interval = get_bits(&s->gb, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
834 s->restart_count = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
835 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
836
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
837 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
838 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
839
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
840 static int mjpeg_decode_app(MJpegDecodeContext *s)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
841 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
842 int len, id, i;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
843
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
844 len = get_bits(&s->gb, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
845 if (len < 5)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
846 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
847 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
848 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
849
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
850 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
851 id = be2me_32(id);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
852 len -= 6;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
853
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
854 if(s->avctx->debug & FF_DEBUG_STARTCODE){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
855 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
856 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
857
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
858 /* buggy AVID, it puts EOI only at every 10th frame */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
859 /* also this fourcc is used by non-avid files too, it holds some
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
860 informations, but it's always present in AVID creates files */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
861 if (id == ff_get_fourcc("AVI1"))
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
862 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
863 /* structure:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
864 4bytes AVI1
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
865 1bytes polarity
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
866 1bytes always zero
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
867 4bytes field_size
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
868 4bytes field_size_less_padding
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
869 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
870 s->buggy_avid = 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
871 // if (s->first_picture)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
872 // printf("mjpeg: workarounding buggy AVID\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
873 i = get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
874 if (i==2) s->bottom_field= 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
875 else if(i==1) s->bottom_field= 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
876 #if 0
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
877 skip_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
878 skip_bits(&s->gb, 32);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
879 skip_bits(&s->gb, 32);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
880 len -= 10;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
881 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
882 // if (s->interlace_polarity)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
883 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
884 goto out;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
885 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
886
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
887 // len -= 2;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
888
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
889 if (id == ff_get_fourcc("JFIF"))
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
890 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
891 int t_w, t_h, v1, v2;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
892 skip_bits(&s->gb, 8); /* the trailing zero-byte */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
893 v1= get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
894 v2= get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
895 skip_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
896
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
897 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
898 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
899
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
900 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
901 av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
902 v1, v2,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
903 s->avctx->sample_aspect_ratio.num,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
904 s->avctx->sample_aspect_ratio.den
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
905 );
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
906
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
907 t_w = get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
908 t_h = get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
909 if (t_w && t_h)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
910 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
911 /* skip thumbnail */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
912 if (len-10-(t_w*t_h*3) > 0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
913 len -= t_w*t_h*3;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
914 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
915 len -= 10;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
916 goto out;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
917 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
918
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
919 if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e'))
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
920 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
921 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
922 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
923 skip_bits(&s->gb, 16); /* version */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
924 skip_bits(&s->gb, 16); /* flags0 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
925 skip_bits(&s->gb, 16); /* flags1 */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
926 skip_bits(&s->gb, 8); /* transform */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
927 len -= 7;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
928 goto out;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
929 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
930
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
931 if (id == ff_get_fourcc("LJIF")){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
932 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
933 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
934 skip_bits(&s->gb, 16); /* version ? */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
935 skip_bits(&s->gb, 16); /* unknwon always 0? */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
936 skip_bits(&s->gb, 16); /* unknwon always 0? */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
937 skip_bits(&s->gb, 16); /* unknwon always 0? */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
938 switch( get_bits(&s->gb, 8)){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
939 case 1:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
940 s->rgb= 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
941 s->pegasus_rct=0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
942 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
943 case 2:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
944 s->rgb= 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
945 s->pegasus_rct=1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
946 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
947 default:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
948 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
949 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
950 len -= 9;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
951 goto out;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
952 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
953
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
954 /* Apple MJPEG-A */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
955 if ((s->start_code == APP1) && (len > (0x28 - 8)))
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
956 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
957 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
958 id = be2me_32(id);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
959 len -= 4;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
960 if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
961 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
962 #if 0
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
963 skip_bits(&s->gb, 32); /* field size */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
964 skip_bits(&s->gb, 32); /* pad field size */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
965 skip_bits(&s->gb, 32); /* next off */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
966 skip_bits(&s->gb, 32); /* quant off */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
967 skip_bits(&s->gb, 32); /* huff off */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
968 skip_bits(&s->gb, 32); /* image off */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
969 skip_bits(&s->gb, 32); /* scan off */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
970 skip_bits(&s->gb, 32); /* data off */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
971 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
972 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
973 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
974 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
975 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
976
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
977 out:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
978 /* slow but needed for extreme adobe jpegs */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
979 if (len < 0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
980 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
981 while(--len > 0)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
982 skip_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
983
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
984 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
985 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
986
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
987 static int mjpeg_decode_com(MJpegDecodeContext *s)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
988 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
989 int len = get_bits(&s->gb, 16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
990 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
991 char *cbuf = av_malloc(len - 1);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
992 if (cbuf) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
993 int i;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
994 for (i = 0; i < len - 2; i++)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
995 cbuf[i] = get_bits(&s->gb, 8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
996 if (i > 0 && cbuf[i-1] == '\n')
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
997 cbuf[i-1] = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
998 else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
999 cbuf[i] = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1000
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1001 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1002 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1003
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1004 /* buggy avid, it puts EOI only at every 10th frame */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1005 if (!strcmp(cbuf, "AVID"))
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1006 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1007 s->buggy_avid = 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1008 // if (s->first_picture)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1009 // printf("mjpeg: workarounding buggy AVID\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1010 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1011 else if(!strcmp(cbuf, "CS=ITU601")){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1012 s->cs_itu601= 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1013 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1014
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1015 av_free(cbuf);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1016 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1017 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1018
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1019 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1020 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1021
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1022 #if 0
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1023 static int valid_marker_list[] =
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1024 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1025 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1026 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1027 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1028 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1029 /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1030 /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1031 /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1032 /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1033 /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1034 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1035 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1036 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1037 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1038 /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1039 /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1040 /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1041 /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1042 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1043 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1044
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1045 /* return the 8 bit start code value and update the search
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1046 state. Return -1 if no start code found */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1047 static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1048 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1049 uint8_t *buf_ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1050 unsigned int v, v2;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1051 int val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1052 #ifdef DEBUG
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1053 int skipped=0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1054 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1055
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1056 buf_ptr = *pbuf_ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1057 while (buf_ptr < buf_end) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1058 v = *buf_ptr++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1059 v2 = *buf_ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1060 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1061 val = *buf_ptr++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1062 goto found;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1063 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1064 #ifdef DEBUG
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1065 skipped++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1066 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1067 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1068 val = -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1069 found:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1070 #ifdef DEBUG
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1071 av_log(NULL, AV_LOG_VERBOSE, "find_marker skipped %d bytes\n", skipped);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1072 #endif
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1073 *pbuf_ptr = buf_ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1074 return val;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1075 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1076
5041
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
1077 int ff_mjpeg_decode_frame(AVCodecContext *avctx,
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1078 void *data, int *data_size,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1079 uint8_t *buf, int buf_size)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1080 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1081 MJpegDecodeContext *s = avctx->priv_data;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1082 uint8_t *buf_end, *buf_ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1083 int start_code;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1084 AVFrame *picture = data;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1085
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1086 buf_ptr = buf;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1087 buf_end = buf + buf_size;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1088 while (buf_ptr < buf_end) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1089 /* find start next marker */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1090 start_code = find_marker(&buf_ptr, buf_end);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1091 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1092 /* EOF */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1093 if (start_code < 0) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1094 goto the_end;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1095 } else {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1096 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1097
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1098 if ((buf_end - buf_ptr) > s->buffer_size)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1099 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1100 av_free(s->buffer);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1101 s->buffer_size = buf_end-buf_ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1102 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1103 av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n",
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1104 s->buffer_size);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1105 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1106
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1107 /* unescape buffer of SOS, use special treatment for JPEG-LS */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1108 if (start_code == SOS && !s->ls)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1109 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1110 uint8_t *src = buf_ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1111 uint8_t *dst = s->buffer;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1112
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1113 while (src<buf_end)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1114 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1115 uint8_t x = *(src++);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1116
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1117 *(dst++) = x;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1118 if (avctx->codec_id != CODEC_ID_THP)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1119 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1120 if (x == 0xff) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1121 while (src < buf_end && x == 0xff)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1122 x = *(src++);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1123
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1124 if (x >= 0xd0 && x <= 0xd7)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1125 *(dst++) = x;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1126 else if (x)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1127 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1128 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1129 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1130 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1131 init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1132
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1133 av_log(avctx, AV_LOG_DEBUG, "escaping removed %d bytes\n",
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1134 (buf_end - buf_ptr) - (dst - s->buffer));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1135 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1136 else if(start_code == SOS && s->ls){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1137 uint8_t *src = buf_ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1138 uint8_t *dst = s->buffer;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1139 int bit_count = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1140 int t = 0, b = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1141 PutBitContext pb;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1142
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1143 s->cur_scan++;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1144
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1145 /* find marker */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1146 while (src + t < buf_end){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1147 uint8_t x = src[t++];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1148 if (x == 0xff){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1149 while((src + t < buf_end) && x == 0xff)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1150 x = src[t++];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1151 if (x & 0x80) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1152 t -= 2;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1153 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1154 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1155 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1156 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1157 bit_count = t * 8;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1158
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1159 init_put_bits(&pb, dst, t);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1160
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1161 /* unescape bitstream */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1162 while(b < t){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1163 uint8_t x = src[b++];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1164 put_bits(&pb, 8, x);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1165 if(x == 0xFF){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1166 x = src[b++];
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1167 put_bits(&pb, 7, x);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1168 bit_count--;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1169 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1170 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1171 flush_put_bits(&pb);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1172
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1173 init_get_bits(&s->gb, dst, bit_count);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1174 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1175 else
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1176 init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1177
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1178 s->start_code = start_code;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1179 if(s->avctx->debug & FF_DEBUG_STARTCODE){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1180 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1181 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1182
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1183 /* process markers */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1184 if (start_code >= 0xd0 && start_code <= 0xd7) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1185 av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1186 /* APP fields */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1187 } else if (start_code >= APP0 && start_code <= APP15) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1188 mjpeg_decode_app(s);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1189 /* Comment */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1190 } else if (start_code == COM){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1191 mjpeg_decode_com(s);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1192 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1193
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1194 switch(start_code) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1195 case SOI:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1196 s->restart_interval = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1197
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1198 s->restart_count = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1199 /* nothing to do on SOI */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1200 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1201 case DQT:
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
1202 ff_mjpeg_decode_dqt(s);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1203 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1204 case DHT:
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
1205 if(ff_mjpeg_decode_dht(s) < 0){
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1206 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1207 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1208 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1209 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1210 case SOF0:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1211 s->lossless=0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1212 s->ls=0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1213 s->progressive=0;
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
1214 if (ff_mjpeg_decode_sof(s) < 0)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1215 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1216 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1217 case SOF2:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1218 s->lossless=0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1219 s->ls=0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1220 s->progressive=1;
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
1221 if (ff_mjpeg_decode_sof(s) < 0)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1222 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1223 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1224 case SOF3:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1225 s->lossless=1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1226 s->ls=0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1227 s->progressive=0;
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
1228 if (ff_mjpeg_decode_sof(s) < 0)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1229 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1230 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1231 case SOF48:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1232 s->lossless=1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1233 s->ls=1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1234 s->progressive=0;
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
1235 if (ff_mjpeg_decode_sof(s) < 0)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1236 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1237 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1238 case LSE:
5041
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
1239 if (!ENABLE_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1240 return -1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1241 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1242 case EOI:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1243 s->cur_scan = 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1244 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1245 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1246 eoi_parser:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1247 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1248 if (s->interlaced) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1249 s->bottom_field ^= 1;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1250 /* if not bottom field, do not output image yet */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1251 if (s->bottom_field == !s->interlace_polarity)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1252 goto not_the_end;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1253 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1254 *picture = s->picture;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1255 *data_size = sizeof(AVFrame);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1256
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1257 if(!s->lossless){
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1258 picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1259 picture->qstride= 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1260 picture->qscale_table= s->qscale_table;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1261 memset(picture->qscale_table, picture->quality, (s->width+15)/16);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1262 if(avctx->debug & FF_DEBUG_QP)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1263 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1264 picture->quality*= FF_QP2LAMBDA;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1265 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1266
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1267 goto the_end;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1268 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1269 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1270 case SOS:
5044
924fdd6175b1 move mjpeg-b decoder in its own file
aurel
parents: 5042
diff changeset
1271 ff_mjpeg_decode_sos(s);
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1272 /* buggy avid puts EOI every 10-20th frame */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1273 /* if restart period is over process EOI */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1274 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1275 goto eoi_parser;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1276 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1277 case DRI:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1278 mjpeg_decode_dri(s);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1279 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1280 case SOF1:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1281 case SOF5:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1282 case SOF6:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1283 case SOF7:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1284 case SOF9:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1285 case SOF10:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1286 case SOF11:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1287 case SOF13:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1288 case SOF14:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1289 case SOF15:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1290 case JPG:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1291 av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1292 break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1293 // default:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1294 // printf("mjpeg: unsupported marker (%x)\n", start_code);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1295 // break;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1296 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1297
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1298 not_the_end:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1299 /* eof process start code */
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1300 buf_ptr += (get_bits_count(&s->gb)+7)/8;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1301 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n",
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1302 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1303 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1304 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1305 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1306 the_end:
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1307 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1308 // return buf_end - buf_ptr;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1309 return buf_ptr - buf;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1310 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1311
5041
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
1312 int ff_mjpeg_decode_end(AVCodecContext *avctx)
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1313 {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1314 MJpegDecodeContext *s = avctx->priv_data;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1315 int i, j;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1316
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1317 av_free(s->buffer);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1318 av_free(s->qscale_table);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1319
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1320 for(i=0;i<2;i++) {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1321 for(j=0;j<4;j++)
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1322 free_vlc(&s->vlcs[i][j]);
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1323 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1324 return 0;
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1325 }
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1326
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1327 AVCodec mjpeg_decoder = {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1328 "mjpeg",
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1329 CODEC_TYPE_VIDEO,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1330 CODEC_ID_MJPEG,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1331 sizeof(MJpegDecodeContext),
5041
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
1332 ff_mjpeg_decode_init,
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1333 NULL,
5041
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
1334 ff_mjpeg_decode_end,
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
1335 ff_mjpeg_decode_frame,
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1336 CODEC_CAP_DR1,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1337 NULL
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1338 };
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1339
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1340 AVCodec thp_decoder = {
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1341 "thp",
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1342 CODEC_TYPE_VIDEO,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1343 CODEC_ID_THP,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1344 sizeof(MJpegDecodeContext),
5041
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
1345 ff_mjpeg_decode_init,
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1346 NULL,
5041
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
1347 ff_mjpeg_decode_end,
01a165280429 allows to disable jpegls decoder
aurel
parents: 5039
diff changeset
1348 ff_mjpeg_decode_frame,
5020
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1349 CODEC_CAP_DR1,
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1350 NULL
eb0ad6423405 split mjpeg.c into an encoder and a decoder file
aurel
parents:
diff changeset
1351 };