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 "mjpeg.h"
29 #include "mjpegdec.h"
30 #include "sp5x.h"
31 
32 
34  void *data, int *got_frame,
35  AVPacket *avpkt)
36 {
37  const uint8_t *buf = avpkt->data;
38  int buf_size = avpkt->size;
39  AVPacket avpkt_recoded;
40  const int qscale = 5;
41  uint8_t *recoded;
42  int i = 0, j = 0;
43 
44  if (!avctx->width || !avctx->height)
45  return -1;
46 
47  recoded = av_mallocz(buf_size + 1024);
48  if (!recoded)
49  return -1;
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_quant_table[qscale * 2], 64);
57  memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 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_init_packet(&avpkt_recoded);
87  avpkt_recoded.data = recoded;
88  avpkt_recoded.size = j;
89  i = ff_mjpeg_decode_frame(avctx, data, got_frame, &avpkt_recoded);
90 
91  av_free(recoded);
92 
93  return i < 0 ? i : avpkt->size;
94 }
95 
96 #if CONFIG_SP5X_DECODER
97 AVCodec ff_sp5x_decoder = {
98  .name = "sp5x",
99  .type = AVMEDIA_TYPE_VIDEO,
100  .id = AV_CODEC_ID_SP5X,
101  .priv_data_size = sizeof(MJpegDecodeContext),
105  .capabilities = CODEC_CAP_DR1,
106  .max_lowres = 3,
107  .long_name = NULL_IF_CONFIG_SMALL("Sunplus JPEG (SP5X)"),
108 };
109 #endif
110 #if CONFIG_AMV_DECODER
111 AVCodec ff_amv_decoder = {
112  .name = "amv",
113  .type = AVMEDIA_TYPE_VIDEO,
114  .id = AV_CODEC_ID_AMV,
115  .priv_data_size = sizeof(MJpegDecodeContext),
119  .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
120 };
121 #endif
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
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.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static const uint8_t sp5x_data_dht[]
Definition: sp5x.h:77
MJPEG encoder and decoder.
uint8_t
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
static const uint8_t sp5x_data_sos[]
Definition: sp5x.h:40
int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mjpegdec.c:1629
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:1867
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
const char * name
Name of the codec implementation.
external API header
int width
picture width / height.
enum AVCodecID codec_id
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
void * buf
Definition: avisynth_c.h:594
struct MJpegDecodeContext MJpegDecodeContext
synthesis window for stochastic i
#define AV_WB16(p, darg)
Definition: intreadwrite.h:237
static const uint8_t sp5x_data_sof[]
Definition: sp5x.h:27
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:83
static int sp5x_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: sp5xdec.c:33
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:56
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
MJPEG decoder.
This structure stores compressed data.
static const uint8_t sp5x_quant_table[20][64]
Definition: sp5x.h:135