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 
27 typedef struct SimbiosisIMXContext {
29  uint32_t pal[256];
30  uint8_t history[32768];
31  int pos;
33 
35 {
36  SimbiosisIMXContext *imx = avctx->priv_data;
37 
38  avctx->pix_fmt = AV_PIX_FMT_PAL8;
39  avctx->width = 320;
40  avctx->height = 160;
41 
42  imx->frame = av_frame_alloc();
43  if (!imx->frame)
44  return AVERROR(ENOMEM);
45 
46  return 0;
47 }
48 
49 static int imx_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
50  int *got_frame, AVPacket *avpkt)
51 {
52  SimbiosisIMXContext *imx = avctx->priv_data;
53  int ret, x, y;
54  AVFrame *frame = imx->frame;
55  GetByteContext gb;
56 
57  if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
58  return ret;
59 
60  if (ff_copy_palette(imx->pal, avpkt, avctx)) {
61 #if FF_API_PALETTE_HAS_CHANGED
65 #endif
67  } else {
69 #if FF_API_PALETTE_HAS_CHANGED
73 #endif
74  }
75 
76  bytestream2_init(&gb, avpkt->data, avpkt->size);
77 
78  memcpy(frame->data[1], imx->pal, AVPALETTE_SIZE);
79 
80  x = 0, y = 0;
81  while (bytestream2_get_bytes_left(&gb) > 0 &&
82  x < 320 && y < 160) {
83  int b = bytestream2_get_byte(&gb);
84  int len = b & 0x3f;
85  int op = b >> 6;
86  int fill;
87 
88  switch (op) {
89  case 3:
90  len = len * 64 + bytestream2_get_byte(&gb);
91  case 0:
92  while (len > 0) {
93  x++;
94  len--;
95  if (x >= 320) {
96  x = 0;
97  y++;
98  }
99  if (y >= 160)
100  break;
101  }
102 
104  break;
105  case 1:
106  if (len == 0) {
107  int offset = bytestream2_get_le16(&gb);
108 
109  if (offset < 0 || offset >= 32768)
110  return AVERROR_INVALIDDATA;
111 
112  len = bytestream2_get_byte(&gb);
113  while (len > 0 && offset < 32768) {
114  frame->data[0][x + y * frame->linesize[0]] = imx->history[offset++];
115  x++;
116  len--;
117  if (x >= 320) {
118  x = 0;
119  y++;
120  }
121  if (y >= 160)
122  break;
123  }
124 
126  } else {
127  while (len > 0) {
128  fill = bytestream2_get_byte(&gb);
129  frame->data[0][x + y * frame->linesize[0]] = fill;
130  if (imx->pos < 32768)
131  imx->history[imx->pos++] = fill;
132  x++;
133  len--;
134  if (x >= 320) {
135  x = 0;
136  y++;
137  }
138  if (y >= 160)
139  break;
140  }
141  }
142  break;
143  case 2:
144  fill = bytestream2_get_byte(&gb);
145 
146  while (len > 0) {
147  frame->data[0][x + y * frame->linesize[0]] = fill;
148  x++;
149  len--;
150  if (x >= 320) {
151  x = 0;
152  y++;
153  }
154  if (y >= 160)
155  break;
156  }
157  break;
158  }
159  }
160 
162 
163  if ((ret = av_frame_ref(rframe, frame)) < 0)
164  return ret;
165 
166  *got_frame = 1;
167 
168  return avpkt->size;
169 }
170 
171 static void imx_decode_flush(AVCodecContext *avctx)
172 {
173  SimbiosisIMXContext *imx = avctx->priv_data;
174 
175  av_frame_unref(imx->frame);
176  imx->pos = 0;
177  memset(imx->pal, 0, sizeof(imx->pal));
178  memset(imx->history, 0, sizeof(imx->history));
179 }
180 
182 {
183  SimbiosisIMXContext *imx = avctx->priv_data;
184 
185  av_frame_free(&imx->frame);
186 
187  return 0;
188 }
189 
191  .p.name = "simbiosis_imx",
192  CODEC_LONG_NAME("Simbiosis Interactive IMX Video"),
193  .p.type = AVMEDIA_TYPE_VIDEO,
195  .priv_data_size = sizeof(SimbiosisIMXContext),
198  .close = imx_decode_close,
199  .flush = imx_decode_flush,
200  .p.capabilities = AV_CODEC_CAP_DR1,
201  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
202 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
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:42
AVFrame::palette_has_changed
attribute_deprecated int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:537
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:130
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
AVPacket::data
uint8_t * data
Definition: packet.h:522
b
#define b
Definition: input.c:41
FFCodec
Definition: codec_internal.h:127
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:616
SimbiosisIMXContext::frame
AVFrame * frame
Definition: imx.c:28
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
imx_decode_flush
static void imx_decode_flush(AVCodecContext *avctx)
Definition: imx.c:171
imx_decode_frame
static int imx_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: imx.c:49
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:118
SimbiosisIMXContext
Definition: imx.c:27
imx_decode_init
static av_cold int imx_decode_init(AVCodecContext *avctx)
Definition: imx.c:34
av_cold
#define av_cold
Definition: attributes.h:90
SimbiosisIMXContext::pal
uint32_t pal[256]
Definition: imx.c:29
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:595
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
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:181
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:446
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
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:523
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:354
codec_internal.h
SimbiosisIMXContext::history
uint8_t history[32768]
Definition: imx.c:30
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
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:576
SimbiosisIMXContext::pos
int pos
Definition: imx.c:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
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:1678
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AV_CODEC_ID_SIMBIOSIS_IMX
@ AV_CODEC_ID_SIMBIOSIS_IMX
Definition: codec_id.h:309
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_simbiosis_imx_decoder
const FFCodec ff_simbiosis_imx_decoder
Definition: imx.c:190
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:389
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:1886