FFmpeg
Loading...
Searching...
No Matches
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
22#include "libavutil/common.h"
23#include "avcodec.h"
24#include "bytestream.h"
25#include "codec_internal.h"
26#include "decode.h"
27
28typedef 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
50static 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;
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->flags |= AV_FRAME_FLAG_KEY;
63 } else {
64 frame->flags &= ~AV_FRAME_FLAG_KEY;
65 }
66
67 bytestream2_init(&gb, avpkt->data, avpkt->size);
68
69 memcpy(frame->data[1], imx->pal, AVPALETTE_SIZE);
70
71 x = 0, y = 0;
72 while (bytestream2_get_bytes_left(&gb) > 0 &&
73 x < 320 && y < 160) {
74 int b = bytestream2_get_byte(&gb);
75 int len = b & 0x3f;
76 int op = b >> 6;
77 int fill;
78
79 switch (op) {
80 case 3:
81 len = len * 64 + bytestream2_get_byte(&gb);
83 case 0:
84 while (len > 0) {
85 x++;
86 len--;
87 if (x >= 320) {
88 x = 0;
89 y++;
90 }
91 if (y >= 160)
92 break;
93 }
94
95 frame->flags &= ~AV_FRAME_FLAG_KEY;
96 break;
97 case 1:
98 if (len == 0) {
99 int offset = bytestream2_get_le16(&gb);
100
101 if (offset < 0 || offset >= 32768)
102 return AVERROR_INVALIDDATA;
103
104 len = bytestream2_get_byte(&gb);
105 while (len > 0 && offset < 32768) {
106 frame->data[0][x + y * frame->linesize[0]] = imx->history[offset++];
107 x++;
108 len--;
109 if (x >= 320) {
110 x = 0;
111 y++;
112 }
113 if (y >= 160)
114 break;
115 }
116
117 frame->flags &= ~AV_FRAME_FLAG_KEY;
118 } else {
119 while (len > 0) {
120 fill = bytestream2_get_byte(&gb);
121 frame->data[0][x + y * frame->linesize[0]] = fill;
122 if (imx->pos < 32768)
123 imx->history[imx->pos++] = fill;
124 x++;
125 len--;
126 if (x >= 320) {
127 x = 0;
128 y++;
129 }
130 if (y >= 160)
131 break;
132 }
133 }
134 break;
135 case 2:
136 fill = bytestream2_get_byte(&gb);
137
138 while (len > 0) {
139 frame->data[0][x + y * frame->linesize[0]] = fill;
140 x++;
141 len--;
142 if (x >= 320) {
143 x = 0;
144 y++;
145 }
146 if (y >= 160)
147 break;
148 }
149 break;
150 }
151 }
152
154
155 if ((ret = av_frame_ref(rframe, frame)) < 0)
156 return ret;
157
158 *got_frame = 1;
159
160 return avpkt->size;
161}
162
164{
165 SimbiosisIMXContext *imx = avctx->priv_data;
166
167 av_frame_unref(imx->frame);
168 imx->pos = 0;
169 memset(imx->pal, 0, sizeof(imx->pal));
170 memset(imx->history, 0, sizeof(imx->history));
171}
172
174{
175 SimbiosisIMXContext *imx = avctx->priv_data;
176
177 av_frame_free(&imx->frame);
178
179 return 0;
180}
181
183 .p.name = "simbiosis_imx",
184 CODEC_LONG_NAME("Simbiosis Interactive IMX Video"),
185 .p.type = AVMEDIA_TYPE_VIDEO,
187 .priv_data_size = sizeof(SimbiosisIMXContext),
190 .close = imx_decode_close,
191 .flush = imx_decode_flush,
192 .p.capabilities = AV_CODEC_CAP_DR1,
193 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
194};
const FFCodec ff_simbiosis_imx_decoder
Definition imx.c:182
Libavcodec external API header.
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal and external API header
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:1906
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:2321
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_SIMBIOSIS_IMX
Definition codec_id.h:304
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
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:278
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
#define b
Definition input.c:43
unsigned offset
Definition libaomenc.c:763
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
static av_cold int imx_decode_close(AVCodecContext *avctx)
Definition imx.c:173
static av_cold int imx_decode_init(AVCodecContext *avctx)
Definition imx.c:35
static int imx_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition imx.c:50
static av_cold void imx_decode_flush(AVCodecContext *avctx)
Definition imx.c:163
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
#define AVPALETTE_SIZE
Definition pixfmt.h:32
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
AVFrame * frame
Definition imx.c:29
uint32_t pal[256]
Definition imx.c:30
uint8_t history[32768]
Definition imx.c:31
int len