FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/imm5.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 0 81 0.0%
Functions: 0 4 0.0%
Branches: 0 34 0.0%

Line Branch Exec Source
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
21 #include "libavutil/attributes_internal.h"
22 #include "libavutil/intreadwrite.h"
23
24 #include "avcodec.h"
25 #include "codec_internal.h"
26
27 typedef struct IMM5Context {
28 AVCodecContext *h264_avctx; // wrapper context for H264
29 AVCodecContext *hevc_avctx; // wrapper context for HEVC
30 } IMM5Context;
31
32 static const struct IMM5_unit {
33 uint8_t bits[14];
34 uint8_t len;
35 } IMM5_units[14] = {
36 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x0B, 0x0F, 0x88 }, 12 },
37 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x83, 0xE2 }, 12 },
38 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x81, 0xE8, 0x80 }, 13 },
39 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x0B, 0x04, 0xA2 }, 12 },
40 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x81, 0x28, 0x80 }, 13 },
41 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x80, 0x92, 0x20 }, 13 },
42 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x0B, 0x0F, 0xC8 }, 13 },
43 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x83, 0xF2 }, 13 },
44 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x81, 0xEC, 0x80 }, 14 },
45 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x0B, 0x04, 0xB2 }, 13 },
46 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x81, 0x2C, 0x80 }, 14 },
47 { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x80, 0x93, 0x20 }, 14 },
48 { { 0x00, 0x00, 0x00, 0x01, 0x68, 0xDE, 0x3C, 0x80 }, 8 },
49 { { 0x00, 0x00, 0x00, 0x01, 0x68, 0xCE, 0x32, 0x28 }, 8 },
50 };
51
52 static av_cold int imm5_init(AVCodecContext *avctx)
53 {
54 IMM5Context *ctx = avctx->priv_data;
55 int ret;
56
57 EXTERN const FFCodec ff_h264_decoder;
58 ctx->h264_avctx = avcodec_alloc_context3(&ff_h264_decoder.p);
59 if (!ctx->h264_avctx)
60 return AVERROR(ENOMEM);
61 ctx->h264_avctx->thread_count = 1;
62 ctx->h264_avctx->flags = avctx->flags;
63 ctx->h264_avctx->flags2 = avctx->flags2;
64 ret = avcodec_open2(ctx->h264_avctx, NULL, NULL);
65 if (ret < 0)
66 return ret;
67
68 EXTERN const FFCodec ff_hevc_decoder;
69 ctx->hevc_avctx = avcodec_alloc_context3(&ff_hevc_decoder.p);
70 if (!ctx->hevc_avctx)
71 return AVERROR(ENOMEM);
72 ctx->hevc_avctx->thread_count = 1;
73 ctx->hevc_avctx->flags = avctx->flags;
74 ctx->hevc_avctx->flags2 = avctx->flags2;
75 ret = avcodec_open2(ctx->hevc_avctx, NULL, NULL);
76 if (ret < 0)
77 return ret;
78
79 return 0;
80 }
81
82 static int imm5_decode_frame(AVCodecContext *avctx, AVFrame *frame,
83 int *got_frame, AVPacket *avpkt)
84 {
85 IMM5Context *ctx = avctx->priv_data;
86 AVCodecContext *codec_avctx = ctx->h264_avctx;
87 int ret;
88
89 if (avpkt->size > 24 && avpkt->data[8] <= 1 && AV_RL32(avpkt->data + 4) + 24ULL <= avpkt->size) {
90 int codec_type = avpkt->data[1];
91 int index = avpkt->data[10];
92 int new_size = AV_RL32(avpkt->data + 4);
93 int offset, off;
94
95 if (codec_type == 0xA) {
96 codec_avctx = ctx->hevc_avctx;
97 } else if (index == 17) {
98 index = 4;
99 } else if (index == 18) {
100 index = 5;
101 }
102
103 if (index >= 1 && index <= 12) {
104 ret = av_packet_make_writable(avpkt);
105 if (ret < 0)
106 return ret;
107
108 index -= 1;
109 off = offset = IMM5_units[index].len;
110 if (codec_type == 2) {
111 offset += IMM5_units[12].len;
112 } else {
113 offset += IMM5_units[13].len;
114 }
115
116 avpkt->data += 24 - offset;
117 avpkt->size = new_size + offset;
118
119 memcpy(avpkt->data, IMM5_units[index].bits, IMM5_units[index].len);
120 if (codec_type == 2) {
121 memcpy(avpkt->data + off, IMM5_units[12].bits, IMM5_units[12].len);
122 } else {
123 memcpy(avpkt->data + off, IMM5_units[13].bits, IMM5_units[13].len);
124 }
125 } else {
126 avpkt->data += 24;
127 avpkt->size -= 24;
128 }
129 }
130
131 ret = avcodec_send_packet(codec_avctx, avpkt);
132 if (ret < 0) {
133 av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
134 return ret;
135 }
136
137 ret = avcodec_receive_frame(codec_avctx, frame);
138 if (ret < 0)
139 return ret;
140
141 avctx->pix_fmt = codec_avctx->pix_fmt;
142 avctx->coded_width = codec_avctx->coded_width;
143 avctx->coded_height = codec_avctx->coded_height;
144 avctx->width = codec_avctx->width;
145 avctx->height = codec_avctx->height;
146 avctx->bit_rate = codec_avctx->bit_rate;
147 avctx->colorspace = codec_avctx->colorspace;
148 avctx->color_range = codec_avctx->color_range;
149 avctx->color_trc = codec_avctx->color_trc;
150 avctx->color_primaries = codec_avctx->color_primaries;
151 avctx->chroma_sample_location = codec_avctx->chroma_sample_location;
152
153 *got_frame = 1;
154
155 return avpkt->size;
156 }
157
158 static void imm5_flush(AVCodecContext *avctx)
159 {
160 IMM5Context *ctx = avctx->priv_data;
161
162 avcodec_flush_buffers(ctx->h264_avctx);
163 avcodec_flush_buffers(ctx->hevc_avctx);
164 }
165
166 static av_cold int imm5_close(AVCodecContext *avctx)
167 {
168 IMM5Context *ctx = avctx->priv_data;
169
170 avcodec_free_context(&ctx->h264_avctx);
171 avcodec_free_context(&ctx->hevc_avctx);
172
173 return 0;
174 }
175
176 const FFCodec ff_imm5_decoder = {
177 .p.name = "imm5",
178 CODEC_LONG_NAME("Infinity IMM5"),
179 .p.type = AVMEDIA_TYPE_VIDEO,
180 .p.id = AV_CODEC_ID_IMM5,
181 .init = imm5_init,
182 FF_CODEC_DECODE_CB(imm5_decode_frame),
183 .close = imm5_close,
184 .flush = imm5_flush,
185 .priv_data_size = sizeof(IMM5Context),
186 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
187 };
188