FFmpeg
imx.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/common.h"
22 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "codec_internal.h"
25 #include "decode.h"
26 #include "internal.h"
27 
28 typedef struct SimbiosisIMXContext {
30  uint32_t pal[256];
31  uint8_t history[32768];
32  int pos;
34 
36 {
37  SimbiosisIMXContext *imx = avctx->priv_data;
38 
39  avctx->pix_fmt = AV_PIX_FMT_PAL8;
40  avctx->width = 320;
41  avctx->height = 160;
42 
43  imx->frame = av_frame_alloc();
44  if (!imx->frame)
45  return AVERROR(ENOMEM);
46 
47  return 0;
48 }
49 
50 static int imx_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
51  int *got_frame, AVPacket *avpkt)
52 {
53  SimbiosisIMXContext *imx = avctx->priv_data;
54  int ret, x, y;
55  AVFrame *frame = imx->frame;
56  GetByteContext gb;
57 
58  if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
59  return ret;
60 
61  if (ff_copy_palette(imx->pal, avpkt, avctx)) {
62  frame->palette_has_changed = 1;
63  frame->key_frame = 1;
64  } else {
65  frame->key_frame = 0;
66  frame->palette_has_changed = 0;
67  }
68 
69  bytestream2_init(&gb, avpkt->data, avpkt->size);
70 
71  memcpy(frame->data[1], imx->pal, AVPALETTE_SIZE);
72 
73  x = 0, y = 0;
74  while (bytestream2_get_bytes_left(&gb) > 0 &&
75  x < 320 && y < 160) {
76  int b = bytestream2_get_byte(&gb);
77  int len = b & 0x3f;
78  int op = b >> 6;
79  int fill;
80 
81  switch (op) {
82  case 3:
83  len = len * 64 + bytestream2_get_byte(&gb);
84  case 0:
85  while (len > 0) {
86  x++;
87  len--;
88  if (x >= 320) {
89  x = 0;
90  y++;
91  }
92  if (y >= 160)
93  break;
94  }
95 
96  frame->key_frame = 0;
97  break;
98  case 1:
99  if (len == 0) {
100  int offset = bytestream2_get_le16(&gb);
101 
102  if (offset < 0 || offset >= 32768)
103  return AVERROR_INVALIDDATA;
104 
105  len = bytestream2_get_byte(&gb);
106  while (len > 0 && offset < 32768) {
107  frame->data[0][x + y * frame->linesize[0]] = imx->history[offset++];
108  x++;
109  len--;
110  if (x >= 320) {
111  x = 0;
112  y++;
113  }
114  if (y >= 160)
115  break;
116  }
117 
118  frame->key_frame = 0;
119  } else {
120  while (len > 0) {
121  fill = bytestream2_get_byte(&gb);
122  frame->data[0][x + y * frame->linesize[0]] = fill;
123  if (imx->pos < 32768)
124  imx->history[imx->pos++] = fill;
125  x++;
126  len--;
127  if (x >= 320) {
128  x = 0;
129  y++;
130  }
131  if (y >= 160)
132  break;
133  }
134  }
135  break;
136  case 2:
137  fill = bytestream2_get_byte(&gb);
138 
139  while (len > 0) {
140  frame->data[0][x + y * frame->linesize[0]] = fill;
141  x++;
142  len--;
143  if (x >= 320) {
144  x = 0;
145  y++;
146  }
147  if (y >= 160)
148  break;
149  }
150  break;
151  }
152  }
153 
154  frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
155 
156  if ((ret = av_frame_ref(rframe, frame)) < 0)
157  return ret;
158 
159  *got_frame = 1;
160 
161  return avpkt->size;
162 }
163 
164 static void imx_decode_flush(AVCodecContext *avctx)
165 {
166  SimbiosisIMXContext *imx = avctx->priv_data;
167 
168  av_frame_unref(imx->frame);
169  imx->pos = 0;
170  memset(imx->pal, 0, sizeof(imx->pal));
171  memset(imx->history, 0, sizeof(imx->history));
172 }
173 
175 {
176  SimbiosisIMXContext *imx = avctx->priv_data;
177 
178  av_frame_free(&imx->frame);
179 
180  return 0;
181 }
182 
184  .p.name = "simbiosis_imx",
185  .p.long_name = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX Video"),
186  .p.type = AVMEDIA_TYPE_VIDEO,
188  .priv_data_size = sizeof(SimbiosisIMXContext),
191  .close = imx_decode_close,
192  .flush = imx_decode_flush,
193  .p.capabilities = AV_CODEC_CAP_DR1,
194  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
196 };
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:39
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
GetByteContext
Definition: bytestream.h:33
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
b
#define b
Definition: input.c:34
FFCodec
Definition: codec_internal.h:112
SimbiosisIMXContext::frame
AVFrame * frame
Definition: imx.c:29
init
static int init
Definition: av_tx.c:47
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
imx_decode_flush
static void imx_decode_flush(AVCodecContext *avctx)
Definition: imx.c:164
imx_decode_frame
static int imx_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: imx.c:50
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
SimbiosisIMXContext
Definition: imx.c:28
imx_decode_init
static av_cold int imx_decode_init(AVCodecContext *avctx)
Definition: imx.c:35
av_cold
#define av_cold
Definition: attributes.h:90
SimbiosisIMXContext::pal
uint32_t pal[256]
Definition: imx.c:30
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
op
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:76
imx_decode_close
static int imx_decode_close(AVCodecContext *avctx)
Definition: imx.c:174
decode.h
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
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
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
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:343
codec_internal.h
SimbiosisIMXContext::history
uint8_t history[32768]
Definition: imx.c:31
offset
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 offset
Definition: writing_filters.txt:86
common.h
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:477
SimbiosisIMXContext::pos
int pos
Definition: imx.c:32
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:562
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:599
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
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:1521
ret
ret
Definition: filter_design.txt:187
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
AVCodecContext
main external API structure.
Definition: avcodec.h:389
AV_CODEC_ID_SIMBIOSIS_IMX
@ AV_CODEC_ID_SIMBIOSIS_IMX
Definition: codec_id.h:308
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_simbiosis_imx_decoder
const FFCodec ff_simbiosis_imx_decoder
Definition: imx.c:183
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:562
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_copy_palette
int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
Check whether the side-data of src contains a palette of size AVPALETTE_SIZE; if so,...
Definition: decode.c:1624