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 
36 {
37  const uint8_t *buf = avpkt->data;
38  int buf_size = avpkt->size;
39  AVBufferRef *buf_recoded;
40  uint8_t *recoded;
41  int i = 0, j = 0;
42 
43  if (!avctx->width || !avctx->height)
44  return -1;
45 
46  buf_recoded = av_buffer_allocz(buf_size + 1024);
47  if (!buf_recoded)
48  return -1;
49  recoded = buf_recoded->data;
50 
51  /* SOI */
52  recoded[j++] = 0xFF;
53  recoded[j++] = 0xD8;
54 
55  memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
56  memcpy(recoded + j + 5, &sp5x_qscale_five_quant_table[0], 64);
57  memcpy(recoded + j + 70, &sp5x_qscale_five_quant_table[1], 64);
58  j += sizeof(sp5x_data_dqt);
59 
60  memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
61  j += sizeof(sp5x_data_dht);
62 
63  memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
64  AV_WB16(recoded+j+5, avctx->coded_height);
65  AV_WB16(recoded+j+7, avctx->coded_width);
66  j += sizeof(sp5x_data_sof);
67 
68  memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
69  j += sizeof(sp5x_data_sos);
70 
71  if(avctx->codec_id==AV_CODEC_ID_AMV)
72  for (i = 2; i < buf_size-2 && j < buf_size+1024-2; i++)
73  recoded[j++] = buf[i];
74  else
75  for (i = 14; i < buf_size && j < buf_size+1024-3; i++)
76  {
77  recoded[j++] = buf[i];
78  if (buf[i] == 0xff)
79  recoded[j++] = 0;
80  }
81 
82  /* EOI */
83  recoded[j++] = 0xFF;
84  recoded[j++] = 0xD9;
85 
86  av_buffer_unref(&avpkt->buf);
87  avpkt->buf = buf_recoded;
88  avpkt->data = recoded;
89  avpkt->size = j;
90 
91  return 0;
92 }
93 
94 #if CONFIG_SP5X_DECODER
95 const FFCodec ff_sp5x_decoder = {
96  .p.name = "sp5x",
97  .p.long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
98  .p.type = AVMEDIA_TYPE_VIDEO,
99  .p.id = AV_CODEC_ID_SP5X,
100  .priv_data_size = sizeof(MJpegDecodeContext),
102  .close = ff_mjpeg_decode_end,
104  .p.capabilities = AV_CODEC_CAP_DR1,
105  .p.max_lowres = 3,
108 };
109 #endif
110 #if CONFIG_AMV_DECODER
111 const FFCodec ff_amv_decoder = {
112  .p.name = "amv",
113  .p.long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
114  .p.type = AVMEDIA_TYPE_VIDEO,
115  .p.id = AV_CODEC_ID_AMV,
116  .priv_data_size = sizeof(MJpegDecodeContext),
118  .close = ff_mjpeg_decode_end,
120  .p.max_lowres = 3,
121  .p.capabilities = AV_CODEC_CAP_DR1,
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:39
sp5x_data_sos
static const uint8_t sp5x_data_sos[]
Definition: sp5x.h:40
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
mjpegdec.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
ff_sp5x_decoder
const FFCodec ff_sp5x_decoder
FFCodec
Definition: codec_internal.h:112
init
static int init
Definition: av_tx.c:47
sp5x.h
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
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
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:577
ff_mjpeg_decode_end
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:2950
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:399
ff_mjpeg_receive_frame
int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: mjpegdec.c:2401
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:357
AV_CODEC_ID_SP5X
@ AV_CODEC_ID_SP5X
Definition: codec_id.h:60
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
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
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:117
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
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: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
FF_CODEC_CAP_SETS_PKT_DTS
#define FF_CODEC_CAP_SETS_PKT_DTS
Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set AVFrame.pkt_dts manually.
Definition: codec_internal.h:46
AVCodecContext::height
int height
Definition: avcodec.h:562
avcodec.h
av_buffer_allocz
AVBufferRef * av_buffer_allocz(size_t size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:93
AV_CODEC_ID_AMV
@ AV_CODEC_ID_AMV
Definition: codec_id.h:157
AVCodecContext
main external API structure.
Definition: avcodec.h:389
FF_CODEC_RECEIVE_FRAME_CB
#define FF_CODEC_RECEIVE_FRAME_CB(func)
Definition: codec_internal.h:260
ff_sp5x_process_packet
int ff_sp5x_process_packet(AVCodecContext *avctx, AVPacket *avpkt)
Definition: sp5xdec.c:35
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:577
sp5x_data_dht
static const uint8_t sp5x_data_dht[]
Definition: sp5x.h:77
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:562
sp5x_qscale_five_quant_table
static const uint8_t sp5x_qscale_five_quant_table[][64]
Definition: sp5x.h:135