FFmpeg
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 "codec_internal.h"
24 #include "decode.h"
25 #include "get_bits.h"
26 
27 typedef struct AvsContext {
29 } AvsContext;
30 
31 typedef enum {
32  AVS_VIDEO = 0x01,
33  AVS_AUDIO = 0x02,
34  AVS_PALETTE = 0x03,
35  AVS_GAME_DATA = 0x04,
36 } AvsBlockType;
37 
38 typedef enum {
39  AVS_I_FRAME = 0x00,
44 
45 
46 static int avs_decode_frame(AVCodecContext * avctx, AVFrame *picture,
47  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 *const p = avs->frame;
54  const uint8_t *table, *vect;
55  uint8_t *out;
56  int i, j, x, y, stride, ret, vect_w = 3, vect_h = 3;
57  AvsVideoSubType sub_type;
59  GetBitContext change_map = {0}; //init to silence warning
60 
61  if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
62  return ret;
64  p->flags &= ~AV_FRAME_FLAG_KEY;
65 
66  out = p->data[0];
67  stride = p->linesize[0];
68 
69  if (buf_end - buf < 4)
70  return AVERROR_INVALIDDATA;
71  sub_type = buf[0];
72  type = buf[1];
73  buf += 4;
74 
75  if (type == AVS_PALETTE) {
76  int first, last;
77  uint32_t *pal = (uint32_t *) p->data[1];
78 
79  first = AV_RL16(buf);
80  last = first + AV_RL16(buf + 2);
81  if (first >= 256 || last > 256 || buf_end - buf < 4 + 4 + 3 * (last - first))
82  return AVERROR_INVALIDDATA;
83  buf += 4;
84  for (i=first; i<last; i++, buf+=3) {
85  pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2);
86  pal[i] |= 0xFFU << 24 | (pal[i] >> 6) & 0x30303;
87  }
88 
89  sub_type = buf[0];
90  type = buf[1];
91  buf += 4;
92  }
93 
94  if (type != AVS_VIDEO)
95  return AVERROR_INVALIDDATA;
96 
97  switch (sub_type) {
98  case AVS_I_FRAME:
100  p->flags |= AV_FRAME_FLAG_KEY;
101  case AVS_P_FRAME_3X3:
102  vect_w = 3;
103  vect_h = 3;
104  break;
105 
106  case AVS_P_FRAME_2X2:
107  vect_w = 2;
108  vect_h = 2;
109  break;
110 
111  case AVS_P_FRAME_2X3:
112  vect_w = 2;
113  vect_h = 3;
114  break;
115 
116  default:
117  return AVERROR_INVALIDDATA;
118  }
119 
120  if (buf_end - buf < 256 * vect_w * vect_h)
121  return AVERROR_INVALIDDATA;
122  table = buf + (256 * vect_w * vect_h);
123  if (sub_type != AVS_I_FRAME) {
124  int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h);
125  if (buf_end - table < map_size)
126  return AVERROR_INVALIDDATA;
127  init_get_bits(&change_map, table, map_size * 8);
128  table += map_size;
129  }
130 
131  for (y=0; y<198; y+=vect_h) {
132  for (x=0; x<318; x+=vect_w) {
133  if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) {
134  if (buf_end - table < 1)
135  return AVERROR_INVALIDDATA;
136  vect = &buf[*table++ * (vect_w * vect_h)];
137  for (j=0; j<vect_w; j++) {
138  out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j];
139  out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j];
140  if (vect_h == 3)
141  out[(y + 2) * stride + x + j] =
142  vect[(2 * vect_w) + j];
143  }
144  }
145  }
146  if (sub_type != AVS_I_FRAME)
147  align_get_bits(&change_map);
148  }
149 
150  if ((ret = av_frame_ref(picture, p)) < 0)
151  return ret;
152  *got_frame = 1;
153 
154  return buf_size;
155 }
156 
158 {
159  AvsContext *s = avctx->priv_data;
160 
161  s->frame = av_frame_alloc();
162  if (!s->frame)
163  return AVERROR(ENOMEM);
164 
165  avctx->pix_fmt = AV_PIX_FMT_PAL8;
166 
167  return ff_set_dimensions(avctx, 318, 198);
168 }
169 
171 {
172  AvsContext *s = avctx->priv_data;
173  av_frame_free(&s->frame);
174  return 0;
175 }
176 
177 
179  .p.name = "avs",
180  CODEC_LONG_NAME("AVS (Audio Video Standard) video"),
181  .p.type = AVMEDIA_TYPE_VIDEO,
182  .p.id = AV_CODEC_ID_AVS,
183  .priv_data_size = sizeof(AvsContext),
186  .close = avs_decode_end,
187  .p.capabilities = AV_CODEC_CAP_DR1,
188 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVS_VIDEO
@ AVS_VIDEO
Definition: avs.c:32
out
FILE * out
Definition: movenc.c:55
AvsContext::frame
AVFrame * frame
Definition: avs.c:28
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
avs_decode_end
static av_cold int avs_decode_end(AVCodecContext *avctx)
Definition: avs.c:170
table
static const uint16_t table[]
Definition: prosumer.c:203
FFCodec
Definition: codec_internal.h:127
AVS_AUDIO
@ AVS_AUDIO
Definition: avs.c:33
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:661
ff_set_dimensions
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:94
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
ff_avs_decoder
const FFCodec ff_avs_decoder
Definition: avs.c:178
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
GetBitContext
Definition: get_bits.h:108
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AvsBlockType
AvsBlockType
Definition: avs.c:31
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
avs_decode_init
static av_cold int avs_decode_init(AVCodecContext *avctx)
Definition: avs.c:157
first
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But first
Definition: rate_distortion.txt:12
AVS_GAME_DATA
@ AVS_GAME_DATA
Definition: avs.c:35
av_cold
#define av_cold
Definition: attributes.h:90
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:640
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVS_P_FRAME_2X2
@ AVS_P_FRAME_2X2
Definition: avs.c:41
decode.h
get_bits.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
AVS_P_FRAME_3X3
@ AVS_P_FRAME_3X3
Definition: avs.c:40
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
avs_decode_frame
static int avs_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition: avs.c:46
AvsVideoSubType
AvsVideoSubType
Definition: avs.c:38
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:491
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
av_frame_ref
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:400
codec_internal.h
AVS_I_FRAME
@ AVS_I_FRAME
Definition: avs.c:39
AvsContext
Definition: avs.c:27
AVS_PALETTE
@ AVS_PALETTE
Definition: avs.c:34
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AV_CODEC_ID_AVS
@ AV_CODEC_ID_AVS
Definition: codec_id.h:134
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
AVS_P_FRAME_2X3
@ AVS_P_FRAME_2X3
Definition: avs.c:42
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1815
ret
ret
Definition: filter_design.txt:187
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:561
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61