FFmpeg
Loading...
Searching...
No Matches
imm6.c
Go to the documentation of this file.
1/*
2 * Infinity IMM6 video decoder
3 *
4 * Copyright (c) 2026 Tom Needham
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
26
27#include "avcodec.h"
28#include "codec_internal.h"
29#include "decode.h"
30
35
37{
38 IMM6Context *s = avctx->priv_data;
39 int ret;
40
42
44 if (!s->h264_avctx)
45 return AVERROR(ENOMEM);
46
47 /* The wrapper delegates threading to the nested H.264 decoder. */
48 s->h264_avctx->thread_count = avctx->thread_count;
49 s->h264_avctx->thread_type = avctx->thread_type;
50 s->h264_avctx->flags = avctx->flags;
51 s->h264_avctx->flags2 = avctx->flags2;
52 s->h264_avctx->max_pixels = avctx->max_pixels;
53 s->h264_avctx->pkt_timebase = avctx->pkt_timebase;
54 s->h264_avctx->skip_frame = avctx->skip_frame;
55 s->h264_avctx->skip_loop_filter = avctx->skip_loop_filter;
56 s->h264_avctx->apply_cropping = 0;
57
58 ret = avcodec_open2(s->h264_avctx, NULL, NULL);
59 if (ret < 0)
60 return ret;
61
62 return 0;
63}
64
66{
67 IMM6Context *s = avctx->priv_data;
68 AVPacket h264_pkt;
69 uint32_t payload_size;
70 int ret;
71
72 if (pkt->size < 24) {
73 av_log(avctx, AV_LOG_ERROR,
74 "Packet too small for IMM6 header: %d bytes\n", pkt->size);
76 }
77
78 if (pkt->data[8] > 1) {
79 av_log(avctx, AV_LOG_ERROR,
80 "Invalid IMM6 mode %u\n", pkt->data[8]);
82 }
83
84 payload_size = AV_RL32(pkt->data + 4);
85 if (payload_size > (unsigned)pkt->size - 24) {
86 av_log(avctx, AV_LOG_ERROR,
87 "Invalid IMM6 payload size %u in %d-byte packet\n",
88 payload_size, pkt->size);
90 }
91
92 h264_pkt = *pkt;
93 h264_pkt.data += 24;
94 h264_pkt.size = payload_size;
95
96 s->h264_avctx->skip_frame = avctx->skip_frame;
97 s->h264_avctx->skip_loop_filter = avctx->skip_loop_filter;
98
99 ret = avcodec_send_packet(s->h264_avctx, &h264_pkt);
100 if (ret < 0 && ret != AVERROR(EAGAIN))
101 av_log(avctx, AV_LOG_ERROR,
102 "Error submitting IMM6 H.264 payload for decoding\n");
103
104 return ret;
105}
106
108{
109 IMM6Context *s = avctx->priv_data;
110 AVCodecContext *h264 = s->h264_avctx;
111
112 avctx->pix_fmt = h264->pix_fmt;
113 avctx->coded_width = h264->coded_width;
114 avctx->coded_height = h264->coded_height;
115 avctx->width = h264->width;
116 avctx->height = h264->height;
117 avctx->delay = h264->delay;
118 avctx->colorspace = h264->colorspace;
119 avctx->color_range = h264->color_range;
120 avctx->color_trc = h264->color_trc;
121 avctx->color_primaries = h264->color_primaries;
124 avctx->has_b_frames = h264->has_b_frames;
125}
126
128{
129 IMM6Context *s = avctx->priv_data;
130 AVPacket pkt = { 0 };
131 int ret;
132
133 for (;;) {
134 ret = avcodec_receive_frame(s->h264_avctx, frame);
135 if (ret >= 0) {
136 imm6_update_context(avctx);
137 return 0;
138 }
139 if (ret == AVERROR_EOF)
140 return ret;
141 if (ret != AVERROR(EAGAIN))
142 return ret;
143
144 if (s->draining)
145 return AVERROR(EAGAIN);
146
147 ret = ff_decode_get_packet(avctx, &pkt);
148 if (ret == AVERROR(EAGAIN))
149 return ret;
150 if (ret == AVERROR_EOF) {
151 s->draining = 1;
152 ret = avcodec_send_packet(s->h264_avctx, NULL);
153 if (ret < 0 && ret != AVERROR_EOF && ret != AVERROR(EAGAIN))
154 return ret;
155 continue;
156 }
157 if (ret < 0)
158 return ret;
159
160 ret = imm6_send_packet(avctx, &pkt);
162 if (ret < 0)
163 return ret;
164 }
165}
166
168{
169 IMM6Context *s = avctx->priv_data;
170
171 avcodec_flush_buffers(s->h264_avctx);
172 s->draining = 0;
173}
174
176{
177 IMM6Context *s = avctx->priv_data;
178
179 avcodec_free_context(&s->h264_avctx);
180
181 return 0;
182}
183
185 .p.name = "imm6",
186 CODEC_LONG_NAME("Infinity IMM6"),
187 .p.type = AVMEDIA_TYPE_VIDEO,
188 .p.id = AV_CODEC_ID_IMM6,
189 .init = imm6_init,
191 .close = imm6_close,
192 .flush = imm6_flush,
193 .priv_data_size = sizeof(IMM6Context),
196};
const FFCodec ff_h264_decoder
Definition h264dec.c:1194
const FFCodec ff_imm6_decoder
Definition imm6.c:184
#define EXTERN
Libavcodec external API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_RECEIVE_FRAME_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...
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
#define NULL
Definition coverity.c:32
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
Definition decode.c:254
static AVPacket * pkt
static AVFrame * frame
int ret
Definition ffplay.c:391
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition avcodec.c:145
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition codec.h:112
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition options.c:149
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition options.c:168
@ AV_CODEC_ID_IMM6
Definition codec_id.h:329
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Alias for avcodec_receive_frame_flags(avctx, frame, 0).
Definition avcodec.c:721
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition decode.c:737
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal codec state / flush internal buffers.
Definition avcodec.c:390
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void imm6_flush(AVCodecContext *avctx)
Definition imm6.c:167
static void imm6_update_context(AVCodecContext *avctx)
Definition imm6.c:107
static av_cold int imm6_init(AVCodecContext *avctx)
Definition imm6.c:36
static int imm6_send_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition imm6.c:65
static av_cold int imm6_close(AVCodecContext *avctx)
Definition imm6.c:175
static int imm6_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Definition imm6.c:127
#define AV_RL32(p)
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
int width
picture width / height.
Definition avcodec.h:605
int flags2
AV_CODEC_FLAG2_*.
Definition avcodec.h:507
int64_t max_pixels
The number of pixels per image to maximally accept.
Definition avcodec.h:1794
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:682
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
Definition avcodec.h:554
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:658
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition avcodec.h:629
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition avcodec.h:710
enum AVDiscard skip_loop_filter
Skip loop filtering for selected frames.
Definition avcodec.h:1660
int thread_type
Which multithreading methods to use.
Definition avcodec.h:1596
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:672
int delay
Codec delay.
Definition avcodec.h:588
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1586
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:665
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition avcodec.h:689
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition avcodec.h:620
void * priv_data
Definition avcodec.h:470
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1674
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
int draining
Definition imm6.c:33
AVCodecContext * h264_avctx
Definition imm6.c:32
#define av_log(a,...)