FFmpeg
sgirledec.c
Go to the documentation of this file.
1 /*
2  * Silicon Graphics RLE 8-bit video 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  * Silicon Graphics RLE 8-bit video decoder
25  * @note Data is packed in rbg323 with rle, contained in mv or mov.
26  * The algorithm and pixfmt are subtly different from SGI images.
27  */
28 
29 #include "libavutil/common.h"
30 
31 #include "avcodec.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34 
36 {
37  avctx->pix_fmt = AV_PIX_FMT_BGR8;
38  return 0;
39 }
40 
41 /**
42  * Convert SGI RBG323 pixel into AV_PIX_FMT_BGR8
43  * SGI RGB data is packed as 8bpp, (msb)3R 2B 3G(lsb)
44  */
45 #define RBG323_TO_BGR8(x) ((((x) << 3) & 0xC0) | \
46  (((x) << 3) & 0x38) | \
47  (((x) >> 5) & 7))
48 static av_always_inline
49 void rbg323_to_bgr8(uint8_t *dst, const uint8_t *src, int size)
50 {
51  int i;
52  for (i = 0; i < size; i++)
53  dst[i] = RBG323_TO_BGR8(src[i]);
54 }
55 
56 /**
57  * @param[out] dst Destination buffer
58  * @param[in] src Source buffer
59  * @param src_size Source buffer size (bytes)
60  * @param width Width of destination buffer (pixels)
61  * @param height Height of destination buffer (pixels)
62  * @param linesize Line size of destination buffer (bytes)
63  *
64  * @return <0 on error
65  */
66 static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst,
67  const uint8_t *src, int src_size,
68  int width, int height, ptrdiff_t linesize)
69 {
70  const uint8_t *src_end = src + src_size;
71  int x = 0, y = 0;
72 
73 #define INC_XY(n) \
74  x += n; \
75  if (x >= width) { \
76  y++; \
77  if (y >= height) \
78  return 0; \
79  x = 0; \
80  }
81 
82  while (src_end - src >= 2) {
83  uint8_t v = *src++;
84  if (v > 0 && v < 0xC0) {
85  do {
86  int length = FFMIN(v, width - x);
87  if (length <= 0)
88  break;
89  memset(dst + y * linesize + x, RBG323_TO_BGR8(*src), length);
90  INC_XY(length);
91  v -= length;
92  } while (v > 0);
93  src++;
94  } else if (v >= 0xC1) {
95  v -= 0xC0;
96  do {
97  int length = FFMIN3(v, width - x, src_end - src);
98  if (src_end - src < length || length <= 0)
99  break;
100  rbg323_to_bgr8(dst + y * linesize + x, src, length);
101  INC_XY(length);
102  src += length;
103  v -= length;
104  } while (v > 0);
105  } else {
106  avpriv_request_sample(avctx, "opcode %d", v);
107  return AVERROR_PATCHWELCOME;
108  }
109  }
110  return 0;
111 }
112 
114  int *got_frame, AVPacket *avpkt)
115 {
116  int ret;
117 
118  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
119  return ret;
120 
121  ret = decode_sgirle8(avctx, frame->data[0], avpkt->data, avpkt->size,
122  avctx->width, avctx->height, frame->linesize[0]);
123  if (ret < 0)
124  return ret;
125 
126  frame->pict_type = AV_PICTURE_TYPE_I;
127  frame->key_frame = 1;
128 
129  *got_frame = 1;
130 
131  return avpkt->size;
132 }
133 
135  .p.name = "sgirle",
136  CODEC_LONG_NAME("Silicon Graphics RLE 8-bit video"),
137  .p.type = AVMEDIA_TYPE_VIDEO,
138  .p.id = AV_CODEC_ID_SGIRLE,
139  .init = sgirle_decode_init,
141  .p.capabilities = AV_CODEC_CAP_DR1,
142 };
rbg323_to_bgr8
static av_always_inline void rbg323_to_bgr8(uint8_t *dst, const uint8_t *src, int size)
Definition: sgirledec.c:49
sgirle_decode_init
static av_cold int sgirle_decode_init(AVCodecContext *avctx)
Definition: sgirledec.c:35
INC_XY
#define INC_XY(n)
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVPacket::data
uint8_t * data
Definition: packet.h:374
FFCodec
Definition: codec_internal.h:127
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AV_PIX_FMT_BGR8
@ AV_PIX_FMT_BGR8
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:83
av_cold
#define av_cold
Definition: attributes.h:90
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1473
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:375
codec_internal.h
size
int size
Definition: twinvq_data.h:10344
height
#define height
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
FFMIN3
#define FFMIN3(a, b, c)
Definition: macros.h:50
common.h
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
AVCodecContext::height
int height
Definition: avcodec.h:598
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:635
avcodec.h
ret
ret
Definition: filter_design.txt:187
RBG323_TO_BGR8
#define RBG323_TO_BGR8(x)
Convert SGI RBG323 pixel into AV_PIX_FMT_BGR8 SGI RGB data is packed as 8bpp, (msb)3R 2B 3G(lsb)
Definition: sgirledec.c:45
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
ff_sgirle_decoder
const FFCodec ff_sgirle_decoder
Definition: sgirledec.c:134
AVCodecContext
main external API structure.
Definition: avcodec.h:426
sgirle_decode_frame
static int sgirle_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: sgirledec.c:113
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AV_CODEC_ID_SGIRLE
@ AV_CODEC_ID_SGIRLE
Definition: codec_id.h:235
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:598
decode_sgirle8
static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst, const uint8_t *src, int src_size, int width, int height, ptrdiff_t linesize)
Definition: sgirledec.c:66