Libav
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 #include "internal.h"
25 
26 
27 typedef struct {
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
48  void *data, 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 *picture = data;
55  AVFrame *const p = avs->frame;
56  const uint8_t *table, *vect;
57  uint8_t *out;
58  int i, j, x, y, stride, ret, vect_w = 3, vect_h = 3;
59  AvsVideoSubType sub_type;
60  AvsBlockType type;
61  GetBitContext change_map;
62 
63  if ((ret = ff_reget_buffer(avctx, p)) < 0) {
64  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
65  return ret;
66  }
68  p->key_frame = 0;
69 
70  out = p->data[0];
71  stride = p->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 *) p->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 AVERROR_INVALIDDATA;
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 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  ff_set_dimensions(avctx, 318, 198);
169 
170  return 0;
171 }
172 
174 {
175  AvsContext *s = avctx->priv_data;
176  av_frame_free(&s->frame);
177  return 0;
178 }
179 
180 
182  .name = "avs",
183  .long_name = NULL_IF_CONFIG_SMALL("AVS (Audio Video Standard) video"),
184  .type = AVMEDIA_TYPE_VIDEO,
185  .id = AV_CODEC_ID_AVS,
186  .priv_data_size = sizeof(AvsContext),
190  .capabilities = CODEC_CAP_DR1,
191 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
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:133
#define AV_RL16(x)
Definition: intreadwrite.h:220
int size
Definition: avcodec.h:974
static int avs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: avs.c:47
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2796
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
uint8_t
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:188
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
bitstream reader API header.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#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:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
Definition: avs.c:27
Definition: avs.c:32
Libavcodec external API header.
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: utils.c:808
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
AVFrame * frame
Definition: avs.c:28
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define av_cold
Definition: attributes.h:66
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:271
uint8_t * data
Definition: avcodec.h:973
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
AvsBlockType
Definition: avs.c:31
static av_cold int avs_decode_init(AVCodecContext *avctx)
Definition: avs.c:159
void * priv_data
Definition: avcodec.h:1092
AvsVideoSubType
Definition: avs.c:38
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
Definition: avs.c:33
common internal api header.
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:416
AVCodec ff_avs_decoder
Definition: avs.c:181
static av_cold int avs_decode_end(AVCodecContext *avctx)
Definition: avs.c:173
This structure stores compressed data.
Definition: avcodec.h:950
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
Predicted.
Definition: avutil.h:254