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 #include "libavutil/attributes.h"
27 
28 typedef struct AvsContext {
30 } AvsContext;
31 
32 typedef enum {
33  AVS_VIDEO = 0x01,
34  AVS_AUDIO = 0x02,
35  AVS_PALETTE = 0x03,
36  AVS_GAME_DATA = 0x04,
37 } AvsBlockType;
38 
39 typedef enum {
40  AVS_I_FRAME = 0x00,
45 
46 
47 static int avs_decode_frame(AVCodecContext * avctx, AVFrame *picture,
48  int *got_frame, AVPacket *avpkt)
49 {
50  const uint8_t *buf = avpkt->data;
51  const uint8_t *buf_end = avpkt->data + avpkt->size;
52  int buf_size = avpkt->size;
53  AvsContext *const avs = avctx->priv_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)) < 0)
63  return ret;
64  p->pict_type = AV_PICTURE_TYPE_P;
65  p->flags &= ~AV_FRAME_FLAG_KEY;
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:
100  p->pict_type = AV_PICTURE_TYPE_I;
101  p->flags |= AV_FRAME_FLAG_KEY;
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 AVERROR_INVALIDDATA;
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  if ((ret = av_frame_ref(picture, p)) < 0)
153  return ret;
154  *got_frame = 1;
155 
156  return buf_size;
157 }
158 
160 {
161  AvsContext *s = avctx->priv_data;
162 
163  s->frame = av_frame_alloc();
164  if (!s->frame)
165  return AVERROR(ENOMEM);
166 
167  avctx->pix_fmt = AV_PIX_FMT_PAL8;
168 
169  return ff_set_dimensions(avctx, 318, 198);
170 }
171 
173 {
174  AvsContext *s = avctx->priv_data;
175  av_frame_free(&s->frame);
176  return 0;
177 }
178 
179 
181  .p.name = "avs",
182  CODEC_LONG_NAME("AVS (Audio Video Standard) video"),
183  .p.type = AVMEDIA_TYPE_VIDEO,
184  .p.id = AV_CODEC_ID_AVS,
185  .priv_data_size = sizeof(AvsContext),
188  .close = avs_decode_end,
189  .p.capabilities = AV_CODEC_CAP_DR1,
190 };
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:33
out
static FILE * out
Definition: movenc.c:55
AvsContext::frame
AVFrame * frame
Definition: avs.c:29
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:435
AVPacket::data
uint8_t * data
Definition: packet.h:595
avs_decode_end
static av_cold int avs_decode_end(AVCodecContext *avctx)
Definition: avs.c:172
table
static const uint16_t table[]
Definition: prosumer.c:203
FFCodec
Definition: codec_internal.h:127
AVS_AUDIO
@ AVS_AUDIO
Definition: avs.c:34
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:91
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:517
ff_avs_decoder
const FFCodec ff_avs_decoder
Definition: avs.c:180
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
GetBitContext
Definition: get_bits.h:109
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:32
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
avs_decode_init
static av_cold int avs_decode_init(AVCodecContext *avctx)
Definition: avs.c:159
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:36
av_cold
#define av_cold
Definition: attributes.h:119
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:650
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:347
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVS_P_FRAME_2X2
@ AVS_P_FRAME_2X2
Definition: avs.c:42
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
av_fallthrough
#define av_fallthrough
Definition: attributes.h:67
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
AVS_P_FRAME_3X3
@ AVS_P_FRAME_3X3
Definition: avs.c:41
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:391
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
avs_decode_frame
static int avs_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition: avs.c:47
AvsVideoSubType
AvsVideoSubType
Definition: avs.c:39
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:551
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:596
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:278
codec_internal.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
AVS_I_FRAME
@ AVS_I_FRAME
Definition: avs.c:40
AvsContext
Definition: avs.c:28
attributes.h
AVS_PALETTE
@ AVS_PALETTE
Definition: avs.c:35
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:179
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:639
AVS_P_FRAME_2X3
@ AVS_P_FRAME_2X3
Definition: avs.c:43
avcodec.h
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:1894
ret
ret
Definition: filter_design.txt:187
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:560
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:439
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:279
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:466
AVPacket
This structure stores compressed data.
Definition: packet.h:572
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
stride
#define stride
Definition: h264pred_template.c:536