2 * Sunplus JPEG decoder (SP5X)
3 * Copyright (c) 2003 Alex Beregszaszi
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @file libavcodec/sp5xdec.c
24 * Sunplus JPEG decoder (SP5X).
33 static int sp5x_decode_frame(AVCodecContext
*avctx
,
34 void *data
, int *data_size
,
37 const uint8_t *buf
= avpkt
->data
;
38 int buf_size
= avpkt
->size
;
39 AVPacket avpkt_recoded
;
41 MJpegDecodeContext
*s
= avctx
->priv_data
;
44 const uint8_t *buf_ptr
;
48 if (!avctx
->width
|| !avctx
->height
)
54 recoded
= av_mallocz(buf_size
+ 1024);
62 memcpy(recoded
+j
, &sp5x_data_dqt
[0], sizeof(sp5x_data_dqt
));
63 memcpy(recoded
+j
+5, &sp5x_quant_table
[qscale
* 2], 64);
64 memcpy(recoded
+j
+70, &sp5x_quant_table
[(qscale
* 2) + 1], 64);
65 j
+= sizeof(sp5x_data_dqt
);
67 memcpy(recoded
+j
, &sp5x_data_dht
[0], sizeof(sp5x_data_dht
));
68 j
+= sizeof(sp5x_data_dht
);
70 memcpy(recoded
+j
, &sp5x_data_sof
[0], sizeof(sp5x_data_sof
));
71 AV_WB16(recoded
+j
+5, avctx
->coded_height
);
72 AV_WB16(recoded
+j
+7, avctx
->coded_width
);
73 j
+= sizeof(sp5x_data_sof
);
75 memcpy(recoded
+j
, &sp5x_data_sos
[0], sizeof(sp5x_data_sos
));
76 j
+= sizeof(sp5x_data_sos
);
78 if(avctx
->codec_id
==CODEC_ID_AMV
)
79 for (i
= 2; i
< buf_size
-2 && j
< buf_size
+1024-2; i
++)
80 recoded
[j
++] = buf
[i
];
82 for (i
= 14; i
< buf_size
&& j
< buf_size
+1024-2; i
++)
84 recoded
[j
++] = buf
[i
];
93 avctx
->flags
&= ~CODEC_FLAG_EMU_EDGE
;
94 av_init_packet(&avpkt_recoded
);
95 avpkt_recoded
.data
= recoded
;
96 avpkt_recoded
.size
= j
;
97 i
= ff_mjpeg_decode_frame(avctx
, data
, data_size
, &avpkt_recoded
);
104 s
->width
= avctx
->coded_width
;
105 s
->height
= avctx
->coded_height
;
106 s
->nb_components
= 3;
107 s
->component_id
[0] = 0;
110 s
->quant_index
[0] = 0;
111 s
->component_id
[1] = 1;
114 s
->quant_index
[1] = 1;
115 s
->component_id
[2] = 2;
118 s
->quant_index
[2] = 1;
122 s
->qscale_table
= av_mallocz((s
->width
+15)/16);
123 avctx
->pix_fmt
= s
->cs_itu601
? PIX_FMT_YUV420P
: PIX_FMT_YUVJ420
;
126 s
->picture
.reference
= 0;
127 if (avctx
->get_buffer(avctx
, &s
->picture
) < 0)
129 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
133 s
->picture
.pict_type
= FF_I_TYPE
;
134 s
->picture
.key_frame
= 1;
136 for (i
= 0; i
< 3; i
++)
137 s
->linesize
[i
] = s
->picture
.linesize
[i
] << s
->interlaced
;
140 for (i
= 0; i
< 64; i
++)
142 j
= s
->scantable
.permutated
[i
];
143 s
->quant_matrixes
[0][j
] = sp5x_quant_table
[(qscale
* 2) + i
];
145 s
->qscale
[0] = FFMAX(
146 s
->quant_matrixes
[0][s
->scantable
.permutated
[1]],
147 s
->quant_matrixes
[0][s
->scantable
.permutated
[8]]) >> 1;
149 for (i
= 0; i
< 64; i
++)
151 j
= s
->scantable
.permutated
[i
];
152 s
->quant_matrixes
[1][j
] = sp5x_quant_table
[(qscale
* 2) + 1 + i
];
154 s
->qscale
[1] = FFMAX(
155 s
->quant_matrixes
[1][s
->scantable
.permutated
[1]],
156 s
->quant_matrixes
[1][s
->scantable
.permutated
[8]]) >> 1;
161 s
->comp_index
[0] = 0;
162 s
->nb_blocks
[0] = s
->h_count
[0] * s
->v_count
[0];
163 s
->h_scount
[0] = s
->h_count
[0];
164 s
->v_scount
[0] = s
->v_count
[0];
168 s
->comp_index
[1] = 1;
169 s
->nb_blocks
[1] = s
->h_count
[1] * s
->v_count
[1];
170 s
->h_scount
[1] = s
->h_count
[1];
171 s
->v_scount
[1] = s
->v_count
[1];
175 s
->comp_index
[2] = 2;
176 s
->nb_blocks
[2] = s
->h_count
[2] * s
->v_count
[2];
177 s
->h_scount
[2] = s
->h_count
[2];
178 s
->v_scount
[2] = s
->v_count
[2];
182 for (i
= 0; i
< 3; i
++)
183 s
->last_dc
[i
] = 1024;
185 s
->mb_width
= (s
->width
* s
->h_max
* 8 -1) / (s
->h_max
* 8);
186 s
->mb_height
= (s
->height
* s
->v_max
* 8 -1) / (s
->v_max
* 8);
188 init_get_bits(&s
->gb
, buf
+14, (buf_size
-14)*8);
190 return mjpeg_decode_scan(s
);
196 AVCodec sp5x_decoder
= {
200 sizeof(MJpegDecodeContext
),
201 ff_mjpeg_decode_init
,
207 .long_name
= NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
210 AVCodec amv_decoder
= {
214 sizeof(MJpegDecodeContext
),
215 ff_mjpeg_decode_init
,
220 .long_name
= NULL_IF_CONFIG_SMALL("AMV Video"),