sgirledec.c
Go to the documentation of this file.
1 /*
2  * SGI RLE 8-bit decoder
3  * Copyright (c) 2012 Peter Ross
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  * @file
24  * SGI RLE 8-bit decoder
25  */
26 
27 #include "avcodec.h"
28 #include "internal.h"
29 
30 typedef struct SGIRLEContext {
33 
35 {
36  SGIRLEContext *s = avctx->priv_data;
37  avctx->pix_fmt = AV_PIX_FMT_BGR8;
38  s->frame = av_frame_alloc();
39  if (!s->frame)
40  return AVERROR(ENOMEM);
41  return 0;
42 }
43 
44 /**
45  * Convert SGI RGB332 pixel into PIX_FMT_BGR8
46  * SGI RGB332 is packed RGB 3:3:2, 8bpp, (msb)3R 2B 3G(lsb)
47  */
48 #define RGB332_TO_BGR8(x) (((x << 3) & 0xC0) | ((x << 3) & 0x38) | ((x >> 5) & 7))
49 
51 {
52  int i;
53  for (i = 0; i < size; i++)
54  dst[i] = RGB332_TO_BGR8(src[i]);
55 }
56 
57 /**
58  * @param[out] dst Destination buffer
59  * @param[in] src Source buffer
60  * @param src_size Source buffer size (bytes)
61  * @param width Width of destination buffer (pixels)
62  * @param height Height of destination buffer (pixels)
63  * @param linesize Line size of destination buffer (bytes)
64  * @return <0 on error
65  */
66 static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize)
67 {
68  const uint8_t *src_end = src + src_size;
69  int x = 0, y = 0;
70 
71 #define INC_XY(n) \
72  x += n; \
73  if (x >= width) { \
74  y++; \
75  if (y >= height) \
76  return 0; \
77  x = 0; \
78  }
79 
80  while (src_end - src >= 2) {
81  uint8_t v = *src++;
82  if (v > 0 && v < 0xC0) {
83  do {
84  int length = FFMIN(v, width - x);
85  memset(dst + y*linesize + x, RGB332_TO_BGR8(*src), length);
86  INC_XY(length);
87  v -= length;
88  } while (v > 0);
89  src++;
90  } else if (v >= 0xC1) {
91  v -= 0xC0;
92  do {
93  int length = FFMIN3(v, width - x, src_end - src);
94  if (src_end - src < length)
95  break;
96  memcpy_rgb332_to_bgr8(dst + y*linesize + x, src, length);
97  INC_XY(length);
98  src += length;
99  v -= length;
100  } while (v > 0);
101  } else {
102  avpriv_request_sample(avctx, "opcode %d", v);
103  return AVERROR_PATCHWELCOME;
104  }
105  }
106  return 0;
107 }
108 
110  void *data, int *got_frame,
111  AVPacket *avpkt)
112 {
113  SGIRLEContext *s = avctx->priv_data;
114  int ret;
115 
116  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
117  return ret;
118 
119  ret = decode_sgirle8(avctx, s->frame->data[0], avpkt->data, avpkt->size, avctx->width, avctx->height, s->frame->linesize[0]);
120  if (ret < 0)
121  return ret;
122 
123  *got_frame = 1;
124  if ((ret = av_frame_ref(data, s->frame)) < 0)
125  return ret;
126 
127  return avpkt->size;
128 }
129 
131 {
132  SGIRLEContext *s = avctx->priv_data;
133 
134  av_frame_free(&s->frame);
135 
136  return 0;
137 }
138 
140  .name = "sgirle",
141  .type = AVMEDIA_TYPE_VIDEO,
142  .id = AV_CODEC_ID_SGIRLE,
143  .priv_data_size = sizeof(SGIRLEContext),
147  .capabilities = CODEC_CAP_DR1,
148  .long_name = NULL_IF_CONFIG_SMALL("SGI RLE 8-bit"),
149 };
float v
const char * s
Definition: avisynth_c.h:668
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static int sgirle_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: sgirledec.c:109
static av_always_inline void memcpy_rgb332_to_bgr8(uint8_t *dst, const uint8_t *src, int size)
Definition: sgirledec.c:50
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
#define av_cold
Definition: attributes.h:78
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
#define FFMIN3(a, b, c)
Definition: common.h:59
static av_cold int sgirle_decode_end(AVCodecContext *avctx)
Definition: sgirledec.c:130
struct SGIRLEContext SGIRLEContext
Discrete Time axis x
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
const char * name
Name of the codec implementation.
external API header
int size
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...
#define FFMIN(a, b)
Definition: common.h:58
ret
Definition: avfilter.c:821
int width
picture width / height.
AVFrame * frame
Definition: sgirledec.c:31
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:87
static int width
Definition: tests/utils.c:158
AVS_Value src
Definition: avisynth_c.h:523
static av_cold int sgirle_decode_init(AVCodecContext *avctx)
Definition: sgirledec.c:34
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
#define INC_XY(n)
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
AVCodec ff_sgirle_decoder
Definition: sgirledec.c:139
synthesis window for stochastic i
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:95
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by an given frame.
Definition: frame.c:228
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal api header.
static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize)
Definition: sgirledec.c:66
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
function y
Definition: D.m:1
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
#define av_always_inline
Definition: attributes.h:41
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
const char int length
Definition: avisynth_c.h:668
#define RGB332_TO_BGR8(x)
Convert SGI RGB332 pixel into PIX_FMT_BGR8 SGI RGB332 is packed RGB 3:3:2, 8bpp, (msb)3R 2B 3G(lsb) ...
Definition: sgirledec.c:48
This structure stores compressed data.