• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/sp5xdec.c

Go to the documentation of this file.
00001 /*
00002  * Sunplus JPEG decoder (SP5X)
00003  * Copyright (c) 2003 Alex Beregszaszi
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "avcodec.h"
00028 #include "mjpeg.h"
00029 #include "mjpegdec.h"
00030 #include "sp5x.h"
00031 
00032 
00033 static int sp5x_decode_frame(AVCodecContext *avctx,
00034                               void *data, int *data_size,
00035                               const uint8_t *buf, int buf_size)
00036 {
00037 #if 0
00038     MJpegDecodeContext *s = avctx->priv_data;
00039 #endif
00040     const int qscale = 5;
00041     const uint8_t *buf_ptr, *buf_end;
00042     uint8_t *recoded;
00043     int i = 0, j = 0;
00044 
00045     if (!avctx->width || !avctx->height)
00046         return -1;
00047 
00048     buf_ptr = buf;
00049     buf_end = buf + buf_size;
00050 
00051 #if 1
00052     recoded = av_mallocz(buf_size + 1024);
00053     if (!recoded)
00054         return -1;
00055 
00056     /* SOI */
00057     recoded[j++] = 0xFF;
00058     recoded[j++] = 0xD8;
00059 
00060     memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
00061     memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64);
00062     memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64);
00063     j += sizeof(sp5x_data_dqt);
00064 
00065     memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
00066     j += sizeof(sp5x_data_dht);
00067 
00068     memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
00069     AV_WB16(recoded+j+5, avctx->coded_height);
00070     AV_WB16(recoded+j+7, avctx->coded_width);
00071     j += sizeof(sp5x_data_sof);
00072 
00073     memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
00074     j += sizeof(sp5x_data_sos);
00075 
00076     if(avctx->codec_id==CODEC_ID_AMV)
00077         for (i = 2; i < buf_size-2 && j < buf_size+1024-2; i++)
00078             recoded[j++] = buf[i];
00079     else
00080     for (i = 14; i < buf_size && j < buf_size+1024-2; i++)
00081     {
00082         recoded[j++] = buf[i];
00083         if (buf[i] == 0xff)
00084             recoded[j++] = 0;
00085     }
00086 
00087     /* EOI */
00088     recoded[j++] = 0xFF;
00089     recoded[j++] = 0xD9;
00090 
00091     avctx->flags &= ~CODEC_FLAG_EMU_EDGE;
00092     i = ff_mjpeg_decode_frame(avctx, data, data_size, recoded, j);
00093 
00094     av_free(recoded);
00095 
00096 #else
00097     /* SOF */
00098     s->bits = 8;
00099     s->width  = avctx->coded_width;
00100     s->height = avctx->coded_height;
00101     s->nb_components = 3;
00102     s->component_id[0] = 0;
00103     s->h_count[0] = 2;
00104     s->v_count[0] = 2;
00105     s->quant_index[0] = 0;
00106     s->component_id[1] = 1;
00107     s->h_count[1] = 1;
00108     s->v_count[1] = 1;
00109     s->quant_index[1] = 1;
00110     s->component_id[2] = 2;
00111     s->h_count[2] = 1;
00112     s->v_count[2] = 1;
00113     s->quant_index[2] = 1;
00114     s->h_max = 2;
00115     s->v_max = 2;
00116 
00117     s->qscale_table = av_mallocz((s->width+15)/16);
00118     avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420;
00119     s->interlaced = 0;
00120 
00121     s->picture.reference = 0;
00122     if (avctx->get_buffer(avctx, &s->picture) < 0)
00123     {
00124         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00125         return -1;
00126     }
00127 
00128     s->picture.pict_type = FF_I_TYPE;
00129     s->picture.key_frame = 1;
00130 
00131     for (i = 0; i < 3; i++)
00132         s->linesize[i] = s->picture.linesize[i] << s->interlaced;
00133 
00134     /* DQT */
00135     for (i = 0; i < 64; i++)
00136     {
00137         j = s->scantable.permutated[i];
00138         s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i];
00139     }
00140     s->qscale[0] = FFMAX(
00141         s->quant_matrixes[0][s->scantable.permutated[1]],
00142         s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1;
00143 
00144     for (i = 0; i < 64; i++)
00145     {
00146         j = s->scantable.permutated[i];
00147         s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i];
00148     }
00149     s->qscale[1] = FFMAX(
00150         s->quant_matrixes[1][s->scantable.permutated[1]],
00151         s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1;
00152 
00153     /* DHT */
00154 
00155     /* SOS */
00156     s->comp_index[0] = 0;
00157     s->nb_blocks[0] = s->h_count[0] * s->v_count[0];
00158     s->h_scount[0] = s->h_count[0];
00159     s->v_scount[0] = s->v_count[0];
00160     s->dc_index[0] = 0;
00161     s->ac_index[0] = 0;
00162 
00163     s->comp_index[1] = 1;
00164     s->nb_blocks[1] = s->h_count[1] * s->v_count[1];
00165     s->h_scount[1] = s->h_count[1];
00166     s->v_scount[1] = s->v_count[1];
00167     s->dc_index[1] = 1;
00168     s->ac_index[1] = 1;
00169 
00170     s->comp_index[2] = 2;
00171     s->nb_blocks[2] = s->h_count[2] * s->v_count[2];
00172     s->h_scount[2] = s->h_count[2];
00173     s->v_scount[2] = s->v_count[2];
00174     s->dc_index[2] = 1;
00175     s->ac_index[2] = 1;
00176 
00177     for (i = 0; i < 3; i++)
00178         s->last_dc[i] = 1024;
00179 
00180     s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8);
00181     s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8);
00182 
00183     init_get_bits(&s->gb, buf+14, (buf_size-14)*8);
00184 
00185     return mjpeg_decode_scan(s);
00186 #endif
00187 
00188     return i;
00189 }
00190 
00191 AVCodec sp5x_decoder = {
00192     "sp5x",
00193     CODEC_TYPE_VIDEO,
00194     CODEC_ID_SP5X,
00195     sizeof(MJpegDecodeContext),
00196     ff_mjpeg_decode_init,
00197     NULL,
00198     ff_mjpeg_decode_end,
00199     sp5x_decode_frame,
00200     CODEC_CAP_DR1,
00201     NULL,
00202     .long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
00203 };
00204 
00205 AVCodec amv_decoder = {
00206     "amv",
00207     CODEC_TYPE_VIDEO,
00208     CODEC_ID_AMV,
00209     sizeof(MJpegDecodeContext),
00210     ff_mjpeg_decode_init,
00211     NULL,
00212     ff_mjpeg_decode_end,
00213     sp5x_decode_frame,
00214     .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
00215 };

Generated on Sat Feb 16 2013 09:23:13 for ffmpeg by  doxygen 1.7.1