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 #include <string.h>
37 
38 #include "libavutil/internal.h"
39 #include "libavutil/intreadwrite.h"
40 #include "avcodec.h"
41 #include "internal.h"
42 
43 
44 static const enum AVPixelFormat pixfmt_rgb24[] = {
46 
47 typedef struct EightBpsContext {
50 
51  unsigned char planes;
52  unsigned char planemap[4];
53 
54  uint32_t pal[256];
56 
57 static int decode_frame(AVCodecContext *avctx, void *data,
58  int *got_frame, AVPacket *avpkt)
59 {
60  const uint8_t *buf = avpkt->data;
61  int buf_size = avpkt->size;
62  EightBpsContext * const c = avctx->priv_data;
63  const unsigned char *encoded = buf;
64  unsigned char *pixptr, *pixptr_end;
65  unsigned int height = avctx->height; // Real image height
66  unsigned int dlen, p, row;
67  const unsigned char *lp, *dp, *ep;
68  unsigned char count;
69  unsigned int px_inc;
70  unsigned int planes = c->planes;
71  unsigned char *planemap = c->planemap;
72 
73  if (c->pic.data[0])
74  avctx->release_buffer(avctx, &c->pic);
75 
76  c->pic.reference = 0;
78  if (ff_get_buffer(avctx, &c->pic) < 0){
79  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
80  return -1;
81  }
82 
83  ep = encoded + buf_size;
84 
85  /* Set data pointer after line lengths */
86  dp = encoded + planes * (height << 1);
87 
88  /* Ignore alpha plane, don't know what to do with it */
89  if (planes == 4)
90  planes--;
91 
92  px_inc = planes + (avctx->pix_fmt == AV_PIX_FMT_RGB32);
93 
94  for (p = 0; p < planes; p++) {
95  /* Lines length pointer for this plane */
96  lp = encoded + p * (height << 1);
97 
98  /* Decode a plane */
99  for (row = 0; row < height; row++) {
100  pixptr = c->pic.data[0] + row * c->pic.linesize[0] + planemap[p];
101  pixptr_end = pixptr + c->pic.linesize[0];
102  if (ep - lp < row * 2 + 2)
103  return AVERROR_INVALIDDATA;
104  dlen = av_be2ne16(*(const unsigned short *)(lp + row * 2));
105  /* Decode a row of this plane */
106  while (dlen > 0) {
107  if (ep - dp <= 1)
108  return -1;
109  if ((count = *dp++) <= 127) {
110  count++;
111  dlen -= count + 1;
112  if (pixptr + count * px_inc > pixptr_end)
113  break;
114  if (ep - dp < count)
115  return -1;
116  while (count--) {
117  *pixptr = *dp++;
118  pixptr += px_inc;
119  }
120  } else {
121  count = 257 - count;
122  if (pixptr + count * px_inc > pixptr_end)
123  break;
124  while (count--) {
125  *pixptr = *dp;
126  pixptr += px_inc;
127  }
128  dp++;
129  dlen -= 2;
130  }
131  }
132  }
133  }
134 
135  if (avctx->bits_per_coded_sample <= 8) {
136  const uint8_t *pal = av_packet_get_side_data(avpkt,
138  NULL);
139  if (pal) {
140  c->pic.palette_has_changed = 1;
141  memcpy(c->pal, pal, AVPALETTE_SIZE);
142  }
143 
144  memcpy (c->pic.data[1], c->pal, AVPALETTE_SIZE);
145  }
146 
147  *got_frame = 1;
148  *(AVFrame*)data = c->pic;
149 
150  /* always report that the buffer was completely consumed */
151  return buf_size;
152 }
153 
155 {
156  EightBpsContext * const c = avctx->priv_data;
157 
158  c->avctx = avctx;
159  c->pic.data[0] = NULL;
160 
161  switch (avctx->bits_per_coded_sample) {
162  case 8:
163  avctx->pix_fmt = AV_PIX_FMT_PAL8;
164  c->planes = 1;
165  c->planemap[0] = 0; // 1st plane is palette indexes
166  break;
167  case 24:
168  avctx->pix_fmt = avctx->get_format(avctx, pixfmt_rgb24);
169  c->planes = 3;
170  c->planemap[0] = 2; // 1st plane is red
171  c->planemap[1] = 1; // 2nd plane is green
172  c->planemap[2] = 0; // 3rd plane is blue
173  break;
174  case 32:
175  avctx->pix_fmt = AV_PIX_FMT_RGB32;
176  c->planes = 4;
177 #if HAVE_BIGENDIAN
178  c->planemap[0] = 1; // 1st plane is red
179  c->planemap[1] = 2; // 2nd plane is green
180  c->planemap[2] = 3; // 3rd plane is blue
181  c->planemap[3] = 0; // 4th plane is alpha???
182 #else
183  c->planemap[0] = 2; // 1st plane is red
184  c->planemap[1] = 1; // 2nd plane is green
185  c->planemap[2] = 0; // 3rd plane is blue
186  c->planemap[3] = 3; // 4th plane is alpha???
187 #endif
188  break;
189  default:
190  av_log(avctx, AV_LOG_ERROR, "Error: Unsupported color depth: %u.\n",
191  avctx->bits_per_coded_sample);
192  return -1;
193  }
194 
195  return 0;
196 }
197 
199 {
200  EightBpsContext * const c = avctx->priv_data;
201 
202  if (c->pic.data[0])
203  avctx->release_buffer(avctx, &c->pic);
204 
205  return 0;
206 }
207 
209  .name = "8bps",
210  .type = AVMEDIA_TYPE_VIDEO,
211  .id = AV_CODEC_ID_8BPS,
212  .priv_data_size = sizeof(EightBpsContext),
213  .init = decode_init,
214  .close = decode_end,
215  .decode = decode_frame,
216  .capabilities = CODEC_CAP_DR1,
217  .long_name = NULL_IF_CONFIG_SMALL("QuickTime 8BPS video"),
218 };
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int buffer_hints
codec suggestion on buffer type if != 0
Definition: avcodec.h:1253
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
int size
Definition: avcodec.h:916
uint32_t pal[256]
Definition: 8bps.c:54
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
AVCodec.
Definition: avcodec.h:2960
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
AVCodec ff_eightbps_decoder
Definition: 8bps.c:208
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2704
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
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:1132
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
common internal API header
unsigned char planes
Definition: 8bps.c:51
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
struct EightBpsContext EightBpsContext
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: 8bps.c:57
NULL
Definition: eval.c:52
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:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
Definition: avcodec.h:1580
static av_cold int decode_init(AVCodecContext *avctx)
Definition: 8bps.c:154
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1246
static enum AVPixelFormat pixfmt_rgb24[]
Definition: 8bps.c:44
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:72
common internal api header.
void * priv_data
Definition: avcodec.h:1382
#define av_be2ne16(x)
Definition: bswap.h:92
static av_cold int decode_end(AVCodecContext *avctx)
Definition: 8bps.c:198
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:190
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898