FFmpeg
sp5xdec.c
Go to the documentation of this file.
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
24  * Sunplus JPEG decoder (SP5X).
25  */
26 
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "mjpeg.h"
30 #include "mjpegdec.h"
31 #include "sp5x.h"
32 
33 
35  void *data, int *got_frame,
36  AVPacket *avpkt)
37 {
38  const uint8_t *buf = avpkt->data;
39  int buf_size = avpkt->size;
40  AVPacket avpkt_recoded;
41  const int qscale = 5;
42  uint8_t *recoded;
43  int i = 0, j = 0;
44 
45  if (!avctx->width || !avctx->height)
46  return -1;
47 
48  recoded = av_mallocz(buf_size + 1024);
49  if (!recoded)
50  return -1;
51 
52  /* SOI */
53  recoded[j++] = 0xFF;
54  recoded[j++] = 0xD8;
55 
56  memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
57  memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64);
58  memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64);
59  j += sizeof(sp5x_data_dqt);
60 
61  memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
62  j += sizeof(sp5x_data_dht);
63 
64  memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
65  AV_WB16(recoded+j+5, avctx->coded_height);
66  AV_WB16(recoded+j+7, avctx->coded_width);
67  j += sizeof(sp5x_data_sof);
68 
69  memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
70  j += sizeof(sp5x_data_sos);
71 
72  if(avctx->codec_id==AV_CODEC_ID_AMV)
73  for (i = 2; i < buf_size-2 && j < buf_size+1024-2; i++)
74  recoded[j++] = buf[i];
75  else
76  for (i = 14; i < buf_size && j < buf_size+1024-3; i++)
77  {
78  recoded[j++] = buf[i];
79  if (buf[i] == 0xff)
80  recoded[j++] = 0;
81  }
82 
83  /* EOI */
84  recoded[j++] = 0xFF;
85  recoded[j++] = 0xD9;
86 
87  av_init_packet(&avpkt_recoded);
88  avpkt_recoded.data = recoded;
89  avpkt_recoded.size = j;
90  i = ff_mjpeg_decode_frame(avctx, data, got_frame, &avpkt_recoded);
91 
92  av_free(recoded);
93 
94  return i < 0 ? i : avpkt->size;
95 }
96 
97 #if CONFIG_SP5X_DECODER
99  .name = "sp5x",
100  .long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
101  .type = AVMEDIA_TYPE_VIDEO,
102  .id = AV_CODEC_ID_SP5X,
103  .priv_data_size = sizeof(MJpegDecodeContext),
105  .close = ff_mjpeg_decode_end,
107  .capabilities = AV_CODEC_CAP_DR1,
108  .max_lowres = 3,
110 };
111 #endif
112 #if CONFIG_AMV_DECODER
114  .name = "amv",
115  .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
116  .type = AVMEDIA_TYPE_VIDEO,
117  .id = AV_CODEC_ID_AMV,
118  .priv_data_size = sizeof(MJpegDecodeContext),
120  .close = ff_mjpeg_decode_end,
122  .max_lowres = 3,
123  .capabilities = AV_CODEC_CAP_DR1,
125 };
126 #endif
AVCodec
AVCodec.
Definition: codec.h:190
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
mjpeg.h
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
sp5x_data_sos
static const uint8_t sp5x_data_sos[]
Definition: sp5x.h:40
mjpegdec.h
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
data
const char data[16]
Definition: mxf.c:91
sp5x.h
ff_mjpeg_decode_init
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:139
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:714
sp5x_quant_table
static const uint8_t sp5x_quant_table[20][64]
Definition: sp5x.h:135
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
ff_mjpeg_decode_end
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:2811
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:536
AV_CODEC_ID_SP5X
@ AV_CODEC_ID_SP5X
Definition: codec_id.h:59
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
ff_sp5x_decoder
AVCodec ff_sp5x_decoder
MJpegDecodeContext
Definition: mjpegdec.h:46
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
AVPacket::size
int size
Definition: packet.h:356
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
sp5x_data_sof
static const uint8_t sp5x_data_sof[]
Definition: sp5x.h:27
sp5x_data_dqt
static const uint8_t sp5x_data_dqt[]
Definition: sp5x.h:53
sp5x_decode_frame
static int sp5x_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: sp5xdec.c:34
ff_mjpeg_decode_frame
int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mjpegdec.c:2330
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
uint8_t
uint8_t
Definition: audio_convert.c:194
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:197
AVCodecContext::height
int height
Definition: avcodec.h:699
avcodec.h
AV_CODEC_ID_AMV
@ AV_CODEC_ID_AMV
Definition: codec_id.h:156
ff_amv_decoder
AVCodec ff_amv_decoder
AVCodecContext
main external API structure.
Definition: avcodec.h:526
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:714
sp5x_data_dht
static const uint8_t sp5x_data_dht[]
Definition: sp5x.h:77
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVPacket
This structure stores compressed data.
Definition: packet.h:332
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:699
av_init_packet
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:35