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 };