1/* 2 * Sunplus JPEG decoder (SP5X) 3 * Copyright (c) 2003 Alex Beregszaszi 4 * 5 * This file is part of FFmpeg. 6 * 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. 11 * 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. 16 * 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 20 */ 21 22/** 23 * @file libavcodec/sp5xdec.c 24 * Sunplus JPEG decoder (SP5X). 25 */ 26 27#include "avcodec.h" 28#include "mjpeg.h" 29#include "mjpegdec.h" 30#include "sp5x.h" 31 32 33static int sp5x_decode_frame(AVCodecContext *avctx, 34 void *data, int *data_size, 35 const uint8_t *buf, int buf_size) 36{ 37#if 0 38 MJpegDecodeContext *s = avctx->priv_data; 39#endif 40 const int qscale = 5; 41 const uint8_t *buf_ptr, *buf_end; 42 uint8_t *recoded; 43 int i = 0, j = 0; 44 45 if (!avctx->width || !avctx->height) 46 return -1; 47 48 buf_ptr = buf; 49 buf_end = buf + buf_size; 50 51#if 1 52 recoded = av_mallocz(buf_size + 1024); 53 if (!recoded) 54 return -1; 55 56 /* SOI */ 57 recoded[j++] = 0xFF; 58 recoded[j++] = 0xD8; 59 60 memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt)); 61 memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64); 62 memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64); 63 j += sizeof(sp5x_data_dqt); 64 65 memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht)); 66 j += sizeof(sp5x_data_dht); 67 68 memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof)); 69 AV_WB16(recoded+j+5, avctx->coded_height); 70 AV_WB16(recoded+j+7, avctx->coded_width); 71 j += sizeof(sp5x_data_sof); 72 73 memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos)); 74 j += sizeof(sp5x_data_sos); 75 76 if(avctx->codec_id==CODEC_ID_AMV) 77 for (i = 2; i < buf_size-2 && j < buf_size+1024-2; i++) 78 recoded[j++] = buf[i]; 79 else 80 for (i = 14; i < buf_size && j < buf_size+1024-2; i++) 81 { 82 recoded[j++] = buf[i]; 83 if (buf[i] == 0xff) 84 recoded[j++] = 0; 85 } 86 87 /* EOI */ 88 recoded[j++] = 0xFF; 89 recoded[j++] = 0xD9; 90 91 avctx->flags &= ~CODEC_FLAG_EMU_EDGE; 92 i = ff_mjpeg_decode_frame(avctx, data, data_size, recoded, j); 93 94 av_free(recoded); 95 96#else 97 /* SOF */ 98 s->bits = 8; 99 s->width = avctx->coded_width; 100 s->height = avctx->coded_height; 101 s->nb_components = 3; 102 s->component_id[0] = 0; 103 s->h_count[0] = 2; 104 s->v_count[0] = 2; 105 s->quant_index[0] = 0; 106 s->component_id[1] = 1; 107 s->h_count[1] = 1; 108 s->v_count[1] = 1; 109 s->quant_index[1] = 1; 110 s->component_id[2] = 2; 111 s->h_count[2] = 1; 112 s->v_count[2] = 1; 113 s->quant_index[2] = 1; 114 s->h_max = 2; 115 s->v_max = 2; 116 117 s->qscale_table = av_mallocz((s->width+15)/16); 118 avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420; 119 s->interlaced = 0; 120 121 s->picture.reference = 0; 122 if (avctx->get_buffer(avctx, &s->picture) < 0) 123 { 124 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 125 return -1; 126 } 127 128 s->picture.pict_type = FF_I_TYPE; 129 s->picture.key_frame = 1; 130 131 for (i = 0; i < 3; i++) 132 s->linesize[i] = s->picture.linesize[i] << s->interlaced; 133 134 /* DQT */ 135 for (i = 0; i < 64; i++) 136 { 137 j = s->scantable.permutated[i]; 138 s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i]; 139 } 140 s->qscale[0] = FFMAX( 141 s->quant_matrixes[0][s->scantable.permutated[1]], 142 s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1; 143 144 for (i = 0; i < 64; i++) 145 { 146 j = s->scantable.permutated[i]; 147 s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i]; 148 } 149 s->qscale[1] = FFMAX( 150 s->quant_matrixes[1][s->scantable.permutated[1]], 151 s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1; 152 153 /* DHT */ 154 155 /* SOS */ 156 s->comp_index[0] = 0; 157 s->nb_blocks[0] = s->h_count[0] * s->v_count[0]; 158 s->h_scount[0] = s->h_count[0]; 159 s->v_scount[0] = s->v_count[0]; 160 s->dc_index[0] = 0; 161 s->ac_index[0] = 0; 162 163 s->comp_index[1] = 1; 164 s->nb_blocks[1] = s->h_count[1] * s->v_count[1]; 165 s->h_scount[1] = s->h_count[1]; 166 s->v_scount[1] = s->v_count[1]; 167 s->dc_index[1] = 1; 168 s->ac_index[1] = 1; 169 170 s->comp_index[2] = 2; 171 s->nb_blocks[2] = s->h_count[2] * s->v_count[2]; 172 s->h_scount[2] = s->h_count[2]; 173 s->v_scount[2] = s->v_count[2]; 174 s->dc_index[2] = 1; 175 s->ac_index[2] = 1; 176 177 for (i = 0; i < 3; i++) 178 s->last_dc[i] = 1024; 179 180 s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8); 181 s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8); 182 183 init_get_bits(&s->gb, buf+14, (buf_size-14)*8); 184 185 return mjpeg_decode_scan(s); 186#endif 187 188 return i; 189} 190 191AVCodec sp5x_decoder = { 192 "sp5x", 193 CODEC_TYPE_VIDEO, 194 CODEC_ID_SP5X, 195 sizeof(MJpegDecodeContext), 196 ff_mjpeg_decode_init, 197 NULL, 198 ff_mjpeg_decode_end, 199 sp5x_decode_frame, 200 CODEC_CAP_DR1, 201 NULL, 202 .long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"), 203}; 204 205AVCodec amv_decoder = { 206 "amv", 207 CODEC_TYPE_VIDEO, 208 CODEC_ID_AMV, 209 sizeof(MJpegDecodeContext), 210 ff_mjpeg_decode_init, 211 NULL, 212 ff_mjpeg_decode_end, 213 sp5x_decode_frame, 214 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"), 215}; 216