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 
34 {
35  const uint8_t *buf = avpkt->data;
36  int buf_size = avpkt->size;
37  AVBufferRef *buf_recoded;
38  uint8_t *recoded;
39  int i = 0, j = 0;
40 
41  if (!avctx->width || !avctx->height)
42  return -1;
43 
44  buf_recoded = av_buffer_allocz(buf_size + 1024);
45  if (!buf_recoded)
46  return -1;
47  recoded = buf_recoded->data;
48 
49  /* SOI */
50  recoded[j++] = 0xFF;
51  recoded[j++] = 0xD8;
52 
53  memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
54  memcpy(recoded + j + 5, &sp5x_qscale_five_quant_table[0], 64);
55  memcpy(recoded + j + 70, &sp5x_qscale_five_quant_table[1], 64);
56  j += sizeof(sp5x_data_dqt);
57 
58  memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
59  j += sizeof(sp5x_data_dht);
60 
61  memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
62  AV_WB16(recoded+j+5, avctx->coded_height);
63  AV_WB16(recoded+j+7, avctx->coded_width);
64  j += sizeof(sp5x_data_sof);
65 
66  memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
67  j += sizeof(sp5x_data_sos);
68 
69  if(avctx->codec_id==AV_CODEC_ID_AMV)
70  for (i = 2; i < buf_size-2 && j < buf_size+1024-2; i++)
71  recoded[j++] = buf[i];
72  else
73  for (i = 14; i < buf_size && j < buf_size+1024-3; i++)
74  {
75  recoded[j++] = buf[i];
76  if (buf[i] == 0xff)
77  recoded[j++] = 0;
78  }
79 
80  /* EOI */
81  recoded[j++] = 0xFF;
82  recoded[j++] = 0xD9;
83 
84  av_buffer_unref(&avpkt->buf);
85  avpkt->buf = buf_recoded;
86  avpkt->data = recoded;
87  avpkt->size = j;
88 
89  return 0;
90 }
91 
92 #if CONFIG_SP5X_DECODER
94  .name = "sp5x",
95  .long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
96  .type = AVMEDIA_TYPE_VIDEO,
97  .id = AV_CODEC_ID_SP5X,
98  .priv_data_size = sizeof(MJpegDecodeContext),
100  .close = ff_mjpeg_decode_end,
102  .capabilities = AV_CODEC_CAP_DR1,
103  .max_lowres = 3,
106 };
107 #endif
108 #if CONFIG_AMV_DECODER
110  .name = "amv",
111  .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
112  .type = AVMEDIA_TYPE_VIDEO,
113  .id = AV_CODEC_ID_AMV,
114  .priv_data_size = sizeof(MJpegDecodeContext),
116  .close = ff_mjpeg_decode_end,
118  .max_lowres = 3,
119  .capabilities = AV_CODEC_CAP_DR1,
122 };
123 #endif
#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
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:125
static const uint8_t sp5x_data_dqt[]
Definition: sp5x.h:53
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:719
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: packet.h:364
static const uint8_t sp5x_data_dht[]
Definition: sp5x.h:77
static CopyRet receive_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
Definition: crystalhd.c:560
AVCodec.
Definition: codec.h:190
MJPEG encoder and decoder.
#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
uint8_t
uint8_t * data
Definition: packet.h:363
static const uint8_t sp5x_data_sos[]
Definition: sp5x.h:40
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:2911
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:346
const char * name
Name of the codec implementation.
Definition: codec.h:197
AVCodec ff_amv_decoder
int width
picture width / height.
Definition: avcodec.h:704
AVCodec ff_sp5x_decoder
static const uint8_t sp5x_qscale_five_quant_table[][64]
Definition: sp5x.h:135
int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: mjpegdec.c:2417
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:541
main external API structure.
Definition: avcodec.h:531
uint8_t * data
The data buffer.
Definition: buffer.h:89
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:83
int ff_sp5x_process_packet(AVCodecContext *avctx, AVPacket *avpkt)
Definition: sp5xdec.c:33
int coded_height
Definition: avcodec.h:719
static const uint8_t sp5x_data_sof[]
Definition: sp5x.h:27
#define FF_CODEC_CAP_SETS_PKT_DTS
Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set AVFrame.pkt_dts manually.
Definition: internal.h:55
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:150
A reference to a data buffer.
Definition: buffer.h:81
common internal api header.
MJPEG decoder.
This structure stores compressed data.
Definition: packet.h:340
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
int i
Definition: input.c:407