FFmpeg
Loading...
Searching...
No Matches
imm5.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 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
24
25#include "avcodec.h"
26#include "codec_internal.h"
27#include "decode.h"
28#include "internal.h"
29
30typedef struct IMM5Context {
31 AVCodecContext *h264_avctx; // wrapper context for H264
32 AVCodecContext *hevc_avctx; // wrapper context for HEVC
35
36static const struct IMM5_unit {
37 uint8_t bits[14];
38 uint8_t len;
39} IMM5_units[14] = {
40 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x0B, 0x0F, 0x88 }, 12 },
41 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x83, 0xE2 }, 12 },
42 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x81, 0xE8, 0x80 }, 13 },
43 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x0B, 0x04, 0xA2 }, 12 },
44 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x81, 0x28, 0x80 }, 13 },
45 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x80, 0x92, 0x20 }, 13 },
46 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x0B, 0x0F, 0xC8 }, 13 },
47 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x83, 0xF2 }, 13 },
48 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x81, 0xEC, 0x80 }, 14 },
49 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x0B, 0x04, 0xB2 }, 13 },
50 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x81, 0x2C, 0x80 }, 14 },
51 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x80, 0x93, 0x20 }, 14 },
52 { { 0x00, 0x00, 0x00, 0x01, 0x68, 0xDE, 0x3C, 0x80 }, 8 },
53 { { 0x00, 0x00, 0x00, 0x01, 0x68, 0xCE, 0x32, 0x28 }, 8 },
54};
55
57{
58 IMM5Context *ctx = avctx->priv_data;
59 int ret;
60
63 if (!ctx->h264_avctx)
64 return AVERROR(ENOMEM);
65 ctx->h264_avctx->thread_count = 1;
66 ctx->h264_avctx->flags = avctx->flags;
67 ctx->h264_avctx->flags2 = avctx->flags2;
68 ctx->h264_avctx->max_pixels = avctx->max_pixels;
69 ret = avcodec_open2(ctx->h264_avctx, NULL, NULL);
70 if (ret < 0)
71 return ret;
72 ctx->codec_avctx = ctx->h264_avctx;
73
76 if (!ctx->hevc_avctx)
77 return AVERROR(ENOMEM);
78 ctx->hevc_avctx->thread_count = 1;
79 ctx->hevc_avctx->flags = avctx->flags;
80 ctx->hevc_avctx->flags2 = avctx->flags2;
81 ctx->hevc_avctx->max_pixels = avctx->max_pixels;
82 ret = avcodec_open2(ctx->hevc_avctx, NULL, NULL);
83 if (ret < 0)
84 return ret;
85
86 return 0;
87}
88
89static int imm5_send_packet(AVCodecContext *avctx, AVPacket *avpkt)
90{
91 IMM5Context *ctx = avctx->priv_data;
92 AVCodecContext *codec_avctx = ctx->h264_avctx;
93 int ret;
94
95 if (avpkt->size > 24 && avpkt->data[8] <= 1 && AV_RL32(avpkt->data + 4) + 24ULL <= avpkt->size) {
96 int codec_type = avpkt->data[1];
97 int index = avpkt->data[10];
98 int new_size = AV_RL32(avpkt->data + 4);
99 int offset, off;
100
101 if (codec_type == 0xA) {
102 codec_avctx = ctx->hevc_avctx;
103 } else if (index == 17) {
104 index = 4;
105 } else if (index == 18) {
106 index = 5;
107 }
108
109 if (index >= 1 && index <= 12) {
110 ret = av_packet_make_writable(avpkt);
111 if (ret < 0)
112 return ret;
113
114 index -= 1;
115 off = offset = IMM5_units[index].len;
116 if (codec_type == 2) {
117 offset += IMM5_units[12].len;
118 } else {
119 offset += IMM5_units[13].len;
120 }
121
122 avpkt->data += 24 - offset;
123 avpkt->size = new_size + offset;
124
125 memcpy(avpkt->data, IMM5_units[index].bits, IMM5_units[index].len);
126 if (codec_type == 2) {
127 memcpy(avpkt->data + off, IMM5_units[12].bits, IMM5_units[12].len);
128 } else {
129 memcpy(avpkt->data + off, IMM5_units[13].bits, IMM5_units[13].len);
130 }
131 } else {
132 avpkt->data += 24;
133 avpkt->size -= 24;
134 }
135 }
136
137 ctx->codec_avctx = codec_avctx;
138 ret = avcodec_send_packet(codec_avctx, avpkt);
139 if (ret < 0)
140 av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
141 return ret;
142}
143
145{
146 IMM5Context *ctx = avctx->priv_data;
147 AVPacket *avpkt = avctx->internal->in_pkt;
148 int ret;
149
150 while ((ret = avcodec_receive_frame(ctx->codec_avctx, frame)) == AVERROR(EAGAIN)) {
151 ret = ff_decode_get_packet(avctx, avpkt);
152 if (ret == AVERROR_EOF) {
153 avcodec_send_packet(ctx->codec_avctx, NULL);
154 continue;
155 }
156 if (ret < 0)
157 return ret;
158 ret = imm5_send_packet(avctx, avpkt);
159 av_packet_unref(avpkt);
160 if (ret < 0)
161 return ret;
162 }
163 if (ret < 0)
164 return ret;
165
166 AVCodecContext *codec_avctx = ctx->codec_avctx;
167 avctx->pix_fmt = codec_avctx->pix_fmt;
168 avctx->coded_width = codec_avctx->coded_width;
169 avctx->coded_height = codec_avctx->coded_height;
170 avctx->width = codec_avctx->width;
171 avctx->height = codec_avctx->height;
172 avctx->bit_rate = codec_avctx->bit_rate;
173 avctx->colorspace = codec_avctx->colorspace;
174 avctx->color_range = codec_avctx->color_range;
175 avctx->color_trc = codec_avctx->color_trc;
176 avctx->color_primaries = codec_avctx->color_primaries;
178
179 return 0;
180}
181
183{
184 IMM5Context *ctx = avctx->priv_data;
185
186 avcodec_flush_buffers(ctx->h264_avctx);
187 avcodec_flush_buffers(ctx->hevc_avctx);
188}
189
191{
192 IMM5Context *ctx = avctx->priv_data;
193
194 avcodec_free_context(&ctx->h264_avctx);
195 avcodec_free_context(&ctx->hevc_avctx);
196
197 return 0;
198}
199
201 .p.name = "imm5",
202 CODEC_LONG_NAME("Infinity IMM5"),
203 .p.type = AVMEDIA_TYPE_VIDEO,
204 .p.id = AV_CODEC_ID_IMM5,
205 .p.capabilities = AV_CODEC_CAP_DELAY,
206 .init = imm5_init,
208 .close = imm5_close,
209 .flush = imm5_flush,
210 .priv_data_size = sizeof(IMM5Context),
211 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
212};
const FFCodec ff_h264_decoder
Definition h264dec.c:1194
const FFCodec ff_imm5_decoder
Definition imm5.c:200
const FFCodec ff_hevc_decoder
Definition hevcdec.c:4501
static AVFormatContext * ctx
#define EXTERN
Libavcodec external API header.
#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 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 AVFrame * frame
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
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_IMM5
Definition codec_id.h:292
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:734
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
int av_packet_make_writable(AVPacket *pkt)
Create a writable reference for the data described by a given packet, avoiding data copy if possible.
Definition packet.c:517
#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
int index
Definition gxfenc.c:90
static int imm5_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Definition imm5.c:144
static const struct IMM5_unit IMM5_units[14]
static int imm5_send_packet(AVCodecContext *avctx, AVPacket *avpkt)
Definition imm5.c:89
static av_cold int imm5_init(AVCodecContext *avctx)
Definition imm5.c:56
static av_cold void imm5_flush(AVCodecContext *avctx)
Definition imm5.c:182
static av_cold int imm5_close(AVCodecContext *avctx)
Definition imm5.c:190
#define AV_RL32(p)
unsigned offset
Definition libaomenc.c:763
common internal api header.
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
enum AVMediaType codec_type
Definition rtp.c:37
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
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:1792
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:681
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:657
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:671
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:664
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition avcodec.h:688
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition avcodec.h:619
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
AVPacket * in_pkt
This packet is used to hold the packet given to decoders implementing the .decode API; it is unused b...
Definition internal.h:83
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
AVCodecContext * hevc_avctx
Definition imm5.c:32
AVCodecContext * h264_avctx
Definition imm5.c:31
AVCodecContext * codec_avctx
Definition imm5.c:33
uint8_t bits[14]
Definition imm5.c:37
uint8_t len
Definition imm5.c:38
#define av_log(a,...)