8bps.c
Go to the documentation of this file.
1 /*
2  * Quicktime Planar RGB (8BPS) Video Decoder
3  * Copyright (C) 2003 Roberto Togni
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
34 #include <stdio.h>
35 #include <stdlib.h>
36 
37 #include "libavutil/intreadwrite.h"
38 #include "avcodec.h"
39 
40 
42 
43 /*
44  * Decoder context
45  */
46 typedef struct EightBpsContext {
47 
50 
51  unsigned char planes;
52  unsigned char planemap[4];
53 
54  uint32_t pal[256];
56 
57 
58 /*
59  *
60  * Decode a frame
61  *
62  */
63 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
64 {
65  const uint8_t *buf = avpkt->data;
66  int buf_size = avpkt->size;
67  EightBpsContext * const c = avctx->priv_data;
68  const unsigned char *encoded = buf;
69  unsigned char *pixptr, *pixptr_end;
70  unsigned int height = avctx->height; // Real image height
71  unsigned int dlen, p, row;
72  const unsigned char *lp, *dp, *ep;
73  unsigned char count;
74  unsigned int px_inc;
75  unsigned int planes = c->planes;
76  unsigned char *planemap = c->planemap;
77 
78  if(c->pic.data[0])
79  avctx->release_buffer(avctx, &c->pic);
80 
81  c->pic.reference = 0;
83  if(avctx->get_buffer(avctx, &c->pic) < 0){
84  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
85  return -1;
86  }
87 
88  ep = encoded + buf_size;
89 
90  /* Set data pointer after line lengths */
91  dp = encoded + planes * (height << 1);
92 
93  /* Ignore alpha plane, don't know what to do with it */
94  if (planes == 4)
95  planes--;
96 
97  px_inc = planes + (avctx->pix_fmt == PIX_FMT_RGB32);
98 
99  for (p = 0; p < planes; p++) {
100  /* Lines length pointer for this plane */
101  lp = encoded + p * (height << 1);
102 
103  /* Decode a plane */
104  for(row = 0; row < height; row++) {
105  pixptr = c->pic.data[0] + row * c->pic.linesize[0] + planemap[p];
106  pixptr_end = pixptr + c->pic.linesize[0];
107  if (ep - lp < row * 2 + 2)
108  return AVERROR_INVALIDDATA;
109  dlen = av_be2ne16(*(const unsigned short *)(lp+row*2));
110  /* Decode a row of this plane */
111  while(dlen > 0) {
112  if(ep - dp <= 1) return -1;
113  if ((count = *dp++) <= 127) {
114  count++;
115  dlen -= count + 1;
116  if (pixptr + count * px_inc > pixptr_end)
117  break;
118  if(ep - dp < count) return -1;
119  while(count--) {
120  *pixptr = *dp++;
121  pixptr += px_inc;
122  }
123  } else {
124  count = 257 - count;
125  if (pixptr + count * px_inc > pixptr_end)
126  break;
127  while(count--) {
128  *pixptr = *dp;
129  pixptr += px_inc;
130  }
131  dp++;
132  dlen -= 2;
133  }
134  }
135  }
136  }
137 
138  if (avctx->bits_per_coded_sample <= 8) {
139  const uint8_t *pal = av_packet_get_side_data(avpkt,
141  NULL);
142  if (pal) {
143  c->pic.palette_has_changed = 1;
144  memcpy(c->pal, pal, AVPALETTE_SIZE);
145  }
146 
147  memcpy (c->pic.data[1], c->pal, AVPALETTE_SIZE);
148  }
149 
150  *data_size = sizeof(AVFrame);
151  *(AVFrame*)data = c->pic;
152 
153  /* always report that the buffer was completely consumed */
154  return buf_size;
155 }
156 
157 
158 /*
159  *
160  * Init 8BPS decoder
161  *
162  */
164 {
165  EightBpsContext * const c = avctx->priv_data;
166 
167  c->avctx = avctx;
168 
169  c->pic.data[0] = NULL;
170 
171  switch (avctx->bits_per_coded_sample) {
172  case 8:
173  avctx->pix_fmt = PIX_FMT_PAL8;
174  c->planes = 1;
175  c->planemap[0] = 0; // 1st plane is palette indexes
176  break;
177  case 24:
178  avctx->pix_fmt = avctx->get_format(avctx, pixfmt_rgb24);
179  c->planes = 3;
180  c->planemap[0] = 2; // 1st plane is red
181  c->planemap[1] = 1; // 2nd plane is green
182  c->planemap[2] = 0; // 3rd plane is blue
183  break;
184  case 32:
185  avctx->pix_fmt = PIX_FMT_RGB32;
186  c->planes = 4;
187 #if HAVE_BIGENDIAN
188  c->planemap[0] = 1; // 1st plane is red
189  c->planemap[1] = 2; // 2nd plane is green
190  c->planemap[2] = 3; // 3rd plane is blue
191  c->planemap[3] = 0; // 4th plane is alpha???
192 #else
193  c->planemap[0] = 2; // 1st plane is red
194  c->planemap[1] = 1; // 2nd plane is green
195  c->planemap[2] = 0; // 3rd plane is blue
196  c->planemap[3] = 3; // 4th plane is alpha???
197 #endif
198  break;
199  default:
200  av_log(avctx, AV_LOG_ERROR, "Error: Unsupported color depth: %u.\n", avctx->bits_per_coded_sample);
201  return -1;
202  }
203 
204  return 0;
205 }
206 
207 
208 
209 
210 /*
211  *
212  * Uninit 8BPS decoder
213  *
214  */
216 {
217  EightBpsContext * const c = avctx->priv_data;
218 
219  if (c->pic.data[0])
220  avctx->release_buffer(avctx, &c->pic);
221 
222  return 0;
223 }
224 
225 
226 
228  .name = "8bps",
229  .type = AVMEDIA_TYPE_VIDEO,
230  .id = CODEC_ID_8BPS,
231  .priv_data_size = sizeof(EightBpsContext),
232  .init = decode_init,
233  .close = decode_end,
234  .decode = decode_frame,
235  .capabilities = CODEC_CAP_DR1,
236  .long_name = NULL_IF_CONFIG_SMALL("QuickTime 8BPS video"),
237 };
int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
Definition: avcodec.h:1726
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define FF_BUFFER_HINTS_VALID
Definition: avcodec.h:880
Audio Video Frame.
Definition: avcodec.h:985
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: 8bps.c:63
static enum PixelFormat pixfmt_rgb24[]
Definition: 8bps.c:41
int buffer_hints
codec suggestion on buffer type if != 0
Definition: avcodec.h:1195
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
enum PixelFormat(* get_format)(struct AVCodecContext *s, const enum PixelFormat *fmt)
callback to negotiate the pixelFormat
Definition: avcodec.h:2135
int size
Definition: avcodec.h:909
uint32_t pal[256]
Definition: 8bps.c:54
AVCodec.
Definition: avcodec.h:3189
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
AVCodec ff_eightbps_decoder
Definition: 8bps.c:227
#define av_cold
Definition: attributes.h:71
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1974
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
AVFrame pic
Definition: 8bps.c:49
AVCodecContext * avctx
Definition: 8bps.c:48
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1074
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
unsigned char planes
Definition: 8bps.c:51
struct AVFrame AVFrame
Audio Video Frame.
struct EightBpsContext EightBpsContext
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:185
NULL
Definition: eval.c:50
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static av_cold int decode_init(AVCodecContext *avctx)
Definition: 8bps.c:163
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1188
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:67
unsigned char planemap[4]
Definition: 8bps.c:52
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:73
PixelFormat
Pixel format.
Definition: pixfmt.h:62
#define AVPALETTE_SIZE
Definition: avcodec.h:3372
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:75
void * priv_data
Definition: avcodec.h:1531
#define av_be2ne16(x)
Definition: bswap.h:92
#define PIX_FMT_RGB32
Definition: pixfmt.h:169
static av_cold int decode_end(AVCodecContext *avctx)
Definition: 8bps.c:215