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 "config_components.h"
28 
29 #include "avcodec.h"
30 #include "codec_internal.h"
31 #include "mjpeg.h"
32 #include "mjpegdec.h"
33 #include "sp5x.h"
34 
35 
37  AVFrame *frame, int *got_frame,
38  AVPacket *avpkt)
39 {
40  const uint8_t *buf = avpkt->data;
41  int buf_size = avpkt->size;
42  uint8_t *recoded;
43  int i = 0, j = 0;
44  int ret;
45 
46  if (!avctx->width || !avctx->height)
47  return -1;
48 
49  recoded = av_mallocz(buf_size + 1024);
50  if (!recoded)
51  return -1;
52 
53  /* SOI */
54  recoded[j++] = 0xFF;
55  recoded[j++] = 0xD8;
56 
57  memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
58  memcpy(recoded + j + 5, &sp5x_qscale_five_quant_table[0], 64);
59  memcpy(recoded + j + 70, &sp5x_qscale_five_quant_table[1], 64);
60  j += sizeof(sp5x_data_dqt);
61 
62  memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
63  j += sizeof(sp5x_data_dht);
64 
65  memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
66  AV_WB16(recoded+j+5, avctx->coded_height);
67  AV_WB16(recoded+j+7, avctx->coded_width);
68  j += sizeof(sp5x_data_sof);
69 
70  memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
71  j += sizeof(sp5x_data_sos);
72 
73  if(avctx->codec_id==AV_CODEC_ID_AMV)
74  for (i = 2; i < buf_size-2 && j < buf_size+1024-2; i++)
75  recoded[j++] = buf[i];
76  else
77  for (i = 14; i < buf_size && j < buf_size+1024-3; i++)
78  {
79  recoded[j++] = buf[i];
80  if (buf[i] == 0xff)
81  recoded[j++] = 0;
82  }
83 
84  /* EOI */
85  recoded[j++] = 0xFF;
86  recoded[j++] = 0xD9;
87 
88  ret = ff_mjpeg_decode_frame_from_buf(avctx, frame, got_frame,
89  avpkt, recoded, j);
90 
91  av_free(recoded);
92 
93  return ret < 0 ? ret : avpkt->size;
94 }
95 
96 #if CONFIG_SP5X_DECODER
97 const FFCodec ff_sp5x_decoder = {
98  .p.name = "sp5x",
99  CODEC_LONG_NAME("Sunplus JPEG (SP5X)"),
100  .p.type = AVMEDIA_TYPE_VIDEO,
101  .p.id = AV_CODEC_ID_SP5X,
102  .priv_data_size = sizeof(MJpegDecodeContext),
104  .close = ff_mjpeg_decode_end,
106  .p.capabilities = AV_CODEC_CAP_DR1,
107  .p.max_lowres = 3,
108  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
109 };
110 #endif
111 #if CONFIG_AMV_DECODER
112 const FFCodec ff_amv_decoder = {
113  .p.name = "amv",
114  CODEC_LONG_NAME("AMV Video"),
115  .p.type = AVMEDIA_TYPE_VIDEO,
116  .p.id = AV_CODEC_ID_AMV,
117  .priv_data_size = sizeof(MJpegDecodeContext),
119  .close = ff_mjpeg_decode_end,
121  .p.max_lowres = 3,
122  .p.capabilities = AV_CODEC_CAP_DR1,
123  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
124 };
125 #endif
mjpeg.h
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: codec_internal.h:42
sp5x_data_sos
static const uint8_t sp5x_data_sos[]
Definition: sp5x.h:40
mjpegdec.h
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVPacket::data
uint8_t * data
Definition: packet.h:374
ff_sp5x_decoder
const FFCodec ff_sp5x_decoder
FFCodec
Definition: codec_internal.h:127
sp5x.h
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
ff_mjpeg_decode_init
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:122
ff_amv_decoder
const FFCodec ff_amv_decoder
ff_mjpeg_decode_frame_from_buf
int ff_mjpeg_decode_frame_from_buf(AVCodecContext *avctx, AVFrame *frame, int *got_frame, const AVPacket *avpkt, const uint8_t *buf, const int buf_size)
Definition: mjpegdec.c:2348
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:613
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
ff_mjpeg_decode_end
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:2882
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:436
AV_CODEC_ID_SP5X
@ AV_CODEC_ID_SP5X
Definition: codec_id.h:62
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
MJpegDecodeContext
Definition: mjpegdec.h:54
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
sp5x_data_sof
static const uint8_t sp5x_data_sof[]
Definition: sp5x.h:27
codec_internal.h
sp5x_data_dqt
static const uint8_t sp5x_data_dqt[]
Definition: sp5x.h:53
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
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:254
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
AVCodecContext::height
int height
Definition: avcodec.h:598
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
sp5x_decode_frame
static int sp5x_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: sp5xdec.c:36
AV_CODEC_ID_AMV
@ AV_CODEC_ID_AMV
Definition: codec_id.h:159
AVCodecContext
main external API structure.
Definition: avcodec.h:426
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:613
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:33
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:598
sp5x_qscale_five_quant_table
static const uint8_t sp5x_qscale_five_quant_table[][64]
Definition: sp5x.h:135