brender_pix.c
Go to the documentation of this file.
1 /*
2  * BRender PIX (.pix) image decoder
3  * Copyright (c) 2012 Aleksi Nurmi
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 /*
23  * Tested against samples from I-War / Independence War and Defiance.
24  * If the PIX file does not contain a palette, the
25  * palette_has_changed property of the AVFrame is set to 0.
26  */
27 
28 #include "libavutil/imgutils.h"
29 #include "avcodec.h"
30 #include "bytestream.h"
31 #include "internal.h"
32 
33 typedef struct BRPixHeader {
34  int format;
35  unsigned int width, height;
36 } BRPixHeader;
37 
39 {
40  unsigned int header_len = bytestream2_get_be32(pgb);
41 
42  out->format = bytestream2_get_byte(pgb);
43  bytestream2_skip(pgb, 2);
44  out->width = bytestream2_get_be16(pgb);
45  out->height = bytestream2_get_be16(pgb);
46 
47  // the header is at least 11 bytes long; we read the first 7
48  if (header_len < 11) {
49  return 0;
50  }
51 
52  // skip the rest of the header
53  bytestream2_skip(pgb, header_len-7);
54 
55  return 1;
56 }
57 
59  void *data, int *got_frame,
60  AVPacket *avpkt)
61 {
62  AVFrame *frame = data;
63 
64  int ret;
65  GetByteContext gb;
66 
67  unsigned int bytes_pp;
68 
69  unsigned int magic[4];
70  unsigned int chunk_type;
71  unsigned int data_len;
72  BRPixHeader hdr;
73 
74  bytestream2_init(&gb, avpkt->data, avpkt->size);
75 
76  magic[0] = bytestream2_get_be32(&gb);
77  magic[1] = bytestream2_get_be32(&gb);
78  magic[2] = bytestream2_get_be32(&gb);
79  magic[3] = bytestream2_get_be32(&gb);
80 
81  if (magic[0] != 0x12 ||
82  magic[1] != 0x8 ||
83  magic[2] != 0x2 ||
84  magic[3] != 0x2) {
85  av_log(avctx, AV_LOG_ERROR, "Not a BRender PIX file\n");
86  return AVERROR_INVALIDDATA;
87  }
88 
89  chunk_type = bytestream2_get_be32(&gb);
90  if (chunk_type != 0x3 && chunk_type != 0x3d) {
91  av_log(avctx, AV_LOG_ERROR, "Invalid chunk type %d\n", chunk_type);
92  return AVERROR_INVALIDDATA;
93  }
94 
95  ret = brpix_decode_header(&hdr, &gb);
96  if (!ret) {
97  av_log(avctx, AV_LOG_ERROR, "Invalid header length\n");
98  return AVERROR_INVALIDDATA;
99  }
100  switch (hdr.format) {
101  case 3:
102  avctx->pix_fmt = AV_PIX_FMT_PAL8;
103  bytes_pp = 1;
104  break;
105  case 4:
106  avctx->pix_fmt = AV_PIX_FMT_RGB555BE;
107  bytes_pp = 2;
108  break;
109  case 5:
110  avctx->pix_fmt = AV_PIX_FMT_RGB565BE;
111  bytes_pp = 2;
112  break;
113  case 6:
114  avctx->pix_fmt = AV_PIX_FMT_RGB24;
115  bytes_pp = 3;
116  break;
117  case 7:
118  avctx->pix_fmt = AV_PIX_FMT_0RGB;
119  bytes_pp = 4;
120  break;
121  case 18:
122  avctx->pix_fmt = AV_PIX_FMT_GRAY8A;
123  bytes_pp = 2;
124  break;
125  default:
126  av_log(avctx, AV_LOG_ERROR, "Format %d is not supported\n",
127  hdr.format);
128  return AVERROR_PATCHWELCOME;
129  }
130 
131  if (av_image_check_size(hdr.width, hdr.height, 0, avctx) < 0)
132  return AVERROR_INVALIDDATA;
133 
134  if (hdr.width != avctx->width || hdr.height != avctx->height)
135  avcodec_set_dimensions(avctx, hdr.width, hdr.height);
136 
137  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
138  return ret;
139 
140  chunk_type = bytestream2_get_be32(&gb);
141 
142  if (avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
143  (chunk_type == 0x3 || chunk_type == 0x3d)) {
144  BRPixHeader palhdr;
145  uint32_t *pal_out = (uint32_t *)frame->data[1];
146  int i;
147 
148  ret = brpix_decode_header(&palhdr, &gb);
149  if (!ret) {
150  av_log(avctx, AV_LOG_ERROR, "Invalid palette header length\n");
151  return AVERROR_INVALIDDATA;
152  }
153  if (palhdr.format != 7) {
154  av_log(avctx, AV_LOG_ERROR, "Palette is not in 0RGB format\n");
155  return AVERROR_INVALIDDATA;
156  }
157 
158  chunk_type = bytestream2_get_be32(&gb);
159  data_len = bytestream2_get_be32(&gb);
160  bytestream2_skip(&gb, 8);
161  if (chunk_type != 0x21 || data_len != 1032 ||
162  bytestream2_get_bytes_left(&gb) < 1032) {
163  av_log(avctx, AV_LOG_ERROR, "Invalid palette data\n");
164  return AVERROR_INVALIDDATA;
165  }
166  // convert 0RGB to machine endian format (ARGB32)
167  for (i = 0; i < 256; ++i) {
168  bytestream2_skipu(&gb, 1);
169  *pal_out++ = (0xFFU << 24) | bytestream2_get_be24u(&gb);
170  }
171  bytestream2_skip(&gb, 8);
172 
173  frame->palette_has_changed = 1;
174 
175  chunk_type = bytestream2_get_be32(&gb);
176  } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
177  uint32_t *pal_out = (uint32_t *)frame->data[1];
178  int i;
179 
180  for (i = 0; i < 256; ++i) {
181  *pal_out++ = (0xFFU << 24) | (i * 0x010101);
182  }
183  frame->palette_has_changed = 1;
184  }
185 
186  data_len = bytestream2_get_be32(&gb);
187  bytestream2_skip(&gb, 8);
188 
189  // read the image data to the buffer
190  {
191  unsigned int bytes_per_scanline = bytes_pp * hdr.width;
192  unsigned int bytes_left = bytestream2_get_bytes_left(&gb);
193 
194  if (chunk_type != 0x21 || data_len != bytes_left ||
195  bytes_left / bytes_per_scanline < hdr.height)
196  {
197  av_log(avctx, AV_LOG_ERROR, "Invalid image data\n");
198  return AVERROR_INVALIDDATA;
199  }
200 
201  av_image_copy_plane(frame->data[0], frame->linesize[0],
202  avpkt->data + bytestream2_tell(&gb),
203  bytes_per_scanline,
204  bytes_per_scanline, hdr.height);
205  }
206 
207  *got_frame = 1;
208 
209  return avpkt->size;
210 }
211 
213  .name = "brender_pix",
214  .type = AVMEDIA_TYPE_VIDEO,
216  .decode = brpix_decode_frame,
217  .capabilities = CODEC_CAP_DR1,
218  .long_name = NULL_IF_CONFIG_SMALL("BRender PIX image"),
219 };
#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:76
misc image utilities
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
static int brpix_decode_header(BRPixHeader *out, GetByteContext *pgb)
Definition: brender_pix.c:38
unsigned int width
Definition: brender_pix.c:35
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:79
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:114
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static int brpix_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: brender_pix.c:58
uint8_t * data
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:165
unsigned int height
Definition: brender_pix.c:35
frame
Definition: stft.m:14
#define U(x)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:159
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
AVCodec ff_brender_pix_decoder
Definition: brender_pix.c:212
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:231
8bit gray, 8bit alpha
Definition: pixfmt.h:141
ret
Definition: avfilter.c:821
int width
picture width / height.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:183
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
x2
Definition: genspecsines3.m:8
synthesis window for stochastic i
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:280
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:116
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
struct BRPixHeader BRPixHeader
common internal api header.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:242
packed RGB 8:8:8, 32bpp, 0RGB0RGB...
Definition: pixfmt.h:214
This structure stores compressed data.
for(j=16;j >0;--j)