avs.c
Go to the documentation of this file.
1 /*
2  * AVS video decoder.
3  * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
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 
22 #include "avcodec.h"
23 #include "get_bits.h"
24 
25 
26 typedef struct {
28 } AvsContext;
29 
30 typedef enum {
31  AVS_VIDEO = 0x01,
32  AVS_AUDIO = 0x02,
33  AVS_PALETTE = 0x03,
34  AVS_GAME_DATA = 0x04,
35 } AvsBlockType;
36 
37 typedef enum {
38  AVS_I_FRAME = 0x00,
43 
44 
45 static int
47  void *data, int *got_frame, AVPacket *avpkt)
48 {
49  const uint8_t *buf = avpkt->data;
50  const uint8_t *buf_end = avpkt->data + avpkt->size;
51  int buf_size = avpkt->size;
52  AvsContext *const avs = avctx->priv_data;
53  AVFrame *picture = data;
54  AVFrame *const p = &avs->picture;
55  const uint8_t *table, *vect;
56  uint8_t *out;
57  int i, j, x, y, stride, vect_w = 3, vect_h = 3;
58  AvsVideoSubType sub_type;
59  AvsBlockType type;
60  GetBitContext change_map;
61 
62  if (avctx->reget_buffer(avctx, p)) {
63  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
64  return -1;
65  }
66  p->reference = 1;
68  p->key_frame = 0;
69 
70  out = avs->picture.data[0];
71  stride = avs->picture.linesize[0];
72 
73  if (buf_end - buf < 4)
74  return AVERROR_INVALIDDATA;
75  sub_type = buf[0];
76  type = buf[1];
77  buf += 4;
78 
79  if (type == AVS_PALETTE) {
80  int first, last;
81  uint32_t *pal = (uint32_t *) avs->picture.data[1];
82 
83  first = AV_RL16(buf);
84  last = first + AV_RL16(buf + 2);
85  if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))
86  return AVERROR_INVALIDDATA;
87  buf += 4;
88  for (i=first; i<last; i++, buf+=3)
89  pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
90 
91  sub_type = buf[0];
92  type = buf[1];
93  buf += 4;
94  }
95 
96  if (type != AVS_VIDEO)
97  return -1;
98 
99  switch (sub_type) {
100  case AVS_I_FRAME:
102  p->key_frame = 1;
103  case AVS_P_FRAME_3X3:
104  vect_w = 3;
105  vect_h = 3;
106  break;
107 
108  case AVS_P_FRAME_2X2:
109  vect_w = 2;
110  vect_h = 2;
111  break;
112 
113  case AVS_P_FRAME_2X3:
114  vect_w = 2;
115  vect_h = 3;
116  break;
117 
118  default:
119  return -1;
120  }
121 
122  if (buf_end - buf < 256 * vect_w * vect_h)
123  return AVERROR_INVALIDDATA;
124  table = buf + (256 * vect_w * vect_h);
125  if (sub_type != AVS_I_FRAME) {
126  int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
127  if (buf_end - table < map_size)
128  return AVERROR_INVALIDDATA;
129  init_get_bits(&change_map, table, map_size * 8);
130  table += map_size;
131  }
132 
133  for (y=0; y<198; y+=vect_h) {
134  for (x=0; x<318; x+=vect_w) {
135  if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
136  if (buf_end - table < 1)
137  return AVERROR_INVALIDDATA;
138  vect = &buf[*table++ * (vect_w * vect_h)];
139  for (j=0; j<vect_w; j++) {
140  out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
141  out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
142  if (vect_h == 3)
143  out[(y + 2) * stride + x + j] =
144  vect[(2 * vect_w) + j];
145  }
146  }
147  }
148  if (sub_type != AVS_I_FRAME)
149  align_get_bits(&change_map);
150  }
151 
152  *picture = avs->picture;
153  *got_frame = 1;
154 
155  return buf_size;
156 }
157 
159 {
160  avctx->pix_fmt = AV_PIX_FMT_PAL8;
161  avcodec_set_dimensions(avctx, 318, 198);
162  return 0;
163 }
164 
166 {
167  AvsContext *s = avctx->priv_data;
168  if (s->picture.data[0])
169  avctx->release_buffer(avctx, &s->picture);
170  return 0;
171 }
172 
173 
175  .name = "avs",
176  .type = AVMEDIA_TYPE_VIDEO,
177  .id = AV_CODEC_ID_AVS,
178  .priv_data_size = sizeof(AvsContext),
182  .capabilities = CODEC_CAP_DR1,
183  .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),
184 };
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
static void align_get_bits(GetBitContext *s)
Definition: get_bits.h:412
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int size
Definition: avcodec.h:916
static int avs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: avs.c:46
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
#define AV_RL16
Definition: intreadwrite.h:42
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2960
int(* reget_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of a frame to get cr buffer for it.
Definition: avcodec.h:2273
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
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
bitstream reader API header.
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
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
Definition: avs.c:26
Definition: avs.c:31
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
static AVFrame * picture
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
AVFrame picture
Definition: avs.c:27
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
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
AvsBlockType
Definition: avs.c:30
static av_cold int avs_decode_init(AVCodecContext *avctx)
Definition: avs.c:158
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
AvsVideoSubType
Definition: avs.c:37
Definition: avs.c:32
void * priv_data
Definition: avcodec.h:1382
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
AVCodec ff_avs_decoder
Definition: avs.c:174
static av_cold int avs_decode_end(AVCodecContext *avctx)
Definition: avs.c:165
This structure stores compressed data.
Definition: avcodec.h:898
Predicted.
Definition: avutil.h:246