FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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 #include "avcodec.h"
23 #include "get_bits.h"
24 #include "internal.h"
25 
26 typedef struct AvsContext {
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->frame;
55  const uint8_t *table, *vect;
56  uint8_t *out;
57  int i, j, x, y, stride, ret, vect_w = 3, vect_h = 3;
58  AvsVideoSubType sub_type;
60  GetBitContext change_map = {0}; //init to silence warning
61 
62  if ((ret = ff_reget_buffer(avctx, p)) < 0)
63  return ret;
65  p->key_frame = 0;
66 
67  out = p->data[0];
68  stride = p->linesize[0];
69 
70  if (buf_end - buf < 4)
71  return AVERROR_INVALIDDATA;
72  sub_type = buf[0];
73  type = buf[1];
74  buf += 4;
75 
76  if (type == AVS_PALETTE) {
77  int first, last;
78  uint32_t *pal = (uint32_t *) p->data[1];
79 
80  first = AV_RL16(buf);
81  last = first + AV_RL16(buf + 2);
82  if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))
83  return AVERROR_INVALIDDATA;
84  buf += 4;
85  for (i=first; i<last; i++, buf+=3) {
86  pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
87  pal[i] |= 0xFFU << 24 | (pal[i] >> 6) & 0x30303;
88  }
89 
90  sub_type = buf[0];
91  type = buf[1];
92  buf += 4;
93  }
94 
95  if (type != AVS_VIDEO)
96  return AVERROR_INVALIDDATA;
97 
98  switch (sub_type) {
99  case AVS_I_FRAME:
101  p->key_frame = 1;
102  case AVS_P_FRAME_3X3:
103  vect_w = 3;
104  vect_h = 3;
105  break;
106 
107  case AVS_P_FRAME_2X2:
108  vect_w = 2;
109  vect_h = 2;
110  break;
111 
112  case AVS_P_FRAME_2X3:
113  vect_w = 2;
114  vect_h = 3;
115  break;
116 
117  default:
118  return AVERROR_INVALIDDATA;
119  }
120 
121  if (buf_end - buf < 256 * vect_w * vect_h)
122  return AVERROR_INVALIDDATA;
123  table = buf + (256 * vect_w * vect_h);
124  if (sub_type != AVS_I_FRAME) {
125  int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
126  if (buf_end - table < map_size)
127  return AVERROR_INVALIDDATA;
128  init_get_bits(&change_map, table, map_size * 8);
129  table += map_size;
130  }
131 
132  for (y=0; y<198; y+=vect_h) {
133  for (x=0; x<318; x+=vect_w) {
134  if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
135  if (buf_end - table < 1)
136  return AVERROR_INVALIDDATA;
137  vect = &buf[*table++ * (vect_w * vect_h)];
138  for (j=0; j<vect_w; j++) {
139  out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
140  out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
141  if (vect_h == 3)
142  out[(y + 2) * stride + x + j] =
143  vect[(2 * vect_w) + j];
144  }
145  }
146  }
147  if (sub_type != AVS_I_FRAME)
148  align_get_bits(&change_map);
149  }
150 
151  if ((ret = av_frame_ref(picture, p)) < 0)
152  return ret;
153  *got_frame = 1;
154 
155  return buf_size;
156 }
157 
159 {
160  AvsContext *s = avctx->priv_data;
161 
162  s->frame = av_frame_alloc();
163  if (!s->frame)
164  return AVERROR(ENOMEM);
165 
166  avctx->pix_fmt = AV_PIX_FMT_PAL8;
167 
168  return ff_set_dimensions(avctx, 318, 198);
169 }
170 
172 {
173  AvsContext *s = avctx->priv_data;
174  av_frame_free(&s->frame);
175  return 0;
176 }
177 
178 
180  .name = "avs",
181  .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),
182  .type = AVMEDIA_TYPE_VIDEO,
183  .id = AV_CODEC_ID_AVS,
184  .priv_data_size = sizeof(AvsContext),
187  .close = avs_decode_end,
188  .capabilities = AV_CODEC_CAP_DR1,
189  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
190 };
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1431
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:1727
AVCodec.
Definition: avcodec.h:3408
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: decode.c:1938
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:441
uint8_t * data
Definition: avcodec.h:1430
bitstream reader API header.
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
static const struct endianess table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
Definition: avs.c:26
Definition: avs.c:31
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
AVFrame * frame
Definition: avs.c:27
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
main external API structure.
Definition: avcodec.h:1518
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:321
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:433
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: frame.h:232
AvsVideoSubType
Definition: avs.c:37
Definition: avs.c:32
common internal api header.
void * priv_data
Definition: avcodec.h:1545
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:296
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:472
FILE * out
Definition: movenc.c:54
AVCodec ff_avs_decoder
Definition: avs.c:179
#define stride
static av_cold int avs_decode_end(AVCodecContext *avctx)
Definition: avs.c:171
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
Predicted.
Definition: avutil.h:275