FFmpeg
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 
21 #include "libavutil/attributes.h"
23 #include "libavutil/intreadwrite.h"
24 
25 #include "avcodec.h"
26 #include "codec_internal.h"
27 
28 typedef struct IMM5Context {
29  AVCodecContext *h264_avctx; // wrapper context for H264
30  AVCodecContext *hevc_avctx; // wrapper context for HEVC
31 } IMM5Context;
32 
33 static const struct IMM5_unit {
34  uint8_t bits[14];
35  uint8_t len;
36 } IMM5_units[14] = {
37  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x0B, 0x0F, 0x88 }, 12 },
38  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x83, 0xE2 }, 12 },
39  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x81, 0xE8, 0x80 }, 13 },
40  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x0B, 0x04, 0xA2 }, 12 },
41  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x81, 0x28, 0x80 }, 13 },
42  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x80, 0x92, 0x20 }, 13 },
43  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x0B, 0x0F, 0xC8 }, 13 },
44  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x83, 0xF2 }, 13 },
45  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x81, 0xEC, 0x80 }, 14 },
46  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x0B, 0x04, 0xB2 }, 13 },
47  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x81, 0x2C, 0x80 }, 14 },
48  { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x80, 0x93, 0x20 }, 14 },
49  { { 0x00, 0x00, 0x00, 0x01, 0x68, 0xDE, 0x3C, 0x80 }, 8 },
50  { { 0x00, 0x00, 0x00, 0x01, 0x68, 0xCE, 0x32, 0x28 }, 8 },
51 };
52 
53 static av_cold int imm5_init(AVCodecContext *avctx)
54 {
55  IMM5Context *ctx = avctx->priv_data;
56  int ret;
57 
60  if (!ctx->h264_avctx)
61  return AVERROR(ENOMEM);
62  ctx->h264_avctx->thread_count = 1;
63  ctx->h264_avctx->flags = avctx->flags;
64  ctx->h264_avctx->flags2 = avctx->flags2;
65  ret = avcodec_open2(ctx->h264_avctx, NULL, NULL);
66  if (ret < 0)
67  return ret;
68 
71  if (!ctx->hevc_avctx)
72  return AVERROR(ENOMEM);
73  ctx->hevc_avctx->thread_count = 1;
74  ctx->hevc_avctx->flags = avctx->flags;
75  ctx->hevc_avctx->flags2 = avctx->flags2;
76  ret = avcodec_open2(ctx->hevc_avctx, NULL, NULL);
77  if (ret < 0)
78  return ret;
79 
80  return 0;
81 }
82 
84  int *got_frame, AVPacket *avpkt)
85 {
86  IMM5Context *ctx = avctx->priv_data;
87  AVCodecContext *codec_avctx = ctx->h264_avctx;
88  int ret;
89 
90  if (avpkt->size > 24 && avpkt->data[8] <= 1 && AV_RL32(avpkt->data + 4) + 24ULL <= avpkt->size) {
91  int codec_type = avpkt->data[1];
92  int index = avpkt->data[10];
93  int new_size = AV_RL32(avpkt->data + 4);
94  int offset, off;
95 
96  if (codec_type == 0xA) {
97  codec_avctx = ctx->hevc_avctx;
98  } else if (index == 17) {
99  index = 4;
100  } else if (index == 18) {
101  index = 5;
102  }
103 
104  if (index >= 1 && index <= 12) {
105  ret = av_packet_make_writable(avpkt);
106  if (ret < 0)
107  return ret;
108 
109  index -= 1;
110  off = offset = IMM5_units[index].len;
111  if (codec_type == 2) {
112  offset += IMM5_units[12].len;
113  } else {
114  offset += IMM5_units[13].len;
115  }
116 
117  avpkt->data += 24 - offset;
118  avpkt->size = new_size + offset;
119 
120  memcpy(avpkt->data, IMM5_units[index].bits, IMM5_units[index].len);
121  if (codec_type == 2) {
122  memcpy(avpkt->data + off, IMM5_units[12].bits, IMM5_units[12].len);
123  } else {
124  memcpy(avpkt->data + off, IMM5_units[13].bits, IMM5_units[13].len);
125  }
126  } else {
127  avpkt->data += 24;
128  avpkt->size -= 24;
129  }
130  }
131 
132  ret = avcodec_send_packet(codec_avctx, avpkt);
133  if (ret < 0) {
134  av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
135  return ret;
136  }
137 
138  ret = avcodec_receive_frame(codec_avctx, frame);
139  if (ret < 0)
140  return ret;
141 
142  avctx->pix_fmt = codec_avctx->pix_fmt;
143  avctx->coded_width = codec_avctx->coded_width;
144  avctx->coded_height = codec_avctx->coded_height;
145  avctx->width = codec_avctx->width;
146  avctx->height = codec_avctx->height;
147  avctx->bit_rate = codec_avctx->bit_rate;
148  avctx->colorspace = codec_avctx->colorspace;
149  avctx->color_range = codec_avctx->color_range;
150  avctx->color_trc = codec_avctx->color_trc;
151  avctx->color_primaries = codec_avctx->color_primaries;
152  avctx->chroma_sample_location = codec_avctx->chroma_sample_location;
153 
154  *got_frame = 1;
155 
156  return avpkt->size;
157 }
158 
159 static av_cold void imm5_flush(AVCodecContext *avctx)
160 {
161  IMM5Context *ctx = avctx->priv_data;
162 
163  avcodec_flush_buffers(ctx->h264_avctx);
164  avcodec_flush_buffers(ctx->hevc_avctx);
165 }
166 
168 {
169  IMM5Context *ctx = avctx->priv_data;
170 
171  avcodec_free_context(&ctx->h264_avctx);
172  avcodec_free_context(&ctx->hevc_avctx);
173 
174  return 0;
175 }
176 
178  .p.name = "imm5",
179  CODEC_LONG_NAME("Infinity IMM5"),
180  .p.type = AVMEDIA_TYPE_VIDEO,
181  .p.id = AV_CODEC_ID_IMM5,
182  .init = imm5_init,
184  .close = imm5_close,
185  .flush = imm5_flush,
186  .priv_data_size = sizeof(IMM5Context),
187  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
188 };
IMM5_unit
Definition: imm5.c:33
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
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
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:659
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:652
AVPacket::data
uint8_t * data
Definition: packet.h:558
FFCodec
Definition: codec_internal.h:127
imm5_decode_frame
static int imm5_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: imm5.c:83
codec_type
enum AVMediaType codec_type
Definition: rtp.c:37
AV_CODEC_ID_IMM5
@ AV_CODEC_ID_IMM5
Definition: codec_id.h:301
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:488
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:607
ff_hevc_decoder
const FFCodec ff_hevc_decoder
Definition: hevcdec.c:4234
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:645
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:106
ff_h264_decoder
const FFCodec ff_h264_decoder
Definition: h264dec.c:1110
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:149
attributes_internal.h
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:346
intreadwrite.h
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1415
avcodec_receive_frame
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
Definition: avcodec.c:708
ctx
AVFormatContext * ctx
Definition: movenc.c:49
IMM5_units
static const struct IMM5_unit IMM5_units[14]
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:331
EXTERN
#define EXTERN
Definition: attributes_internal.h:34
imm5_flush
static av_cold void imm5_flush(AVCodecContext *avctx)
Definition: imm5.c:159
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:669
IMM5Context
Definition: imm5.c:28
avcodec_free_context
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:164
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:481
avcodec_open2
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
index
int index
Definition: gxfenc.c:90
IMM5_unit::len
uint8_t len
Definition: imm5.c:35
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:495
IMM5Context::hevc_avctx
AVCodecContext * hevc_avctx
Definition: imm5.c:30
AVPacket::size
int size
Definition: packet.h:559
codec_internal.h
IMM5_unit::bits
uint8_t bits[14]
Definition: imm5.c:34
IMM5Context::h264_avctx
AVCodecContext * h264_avctx
Definition: imm5.c:29
size
int size
Definition: twinvq_data.h:10344
ff_imm5_decoder
const FFCodec ff_imm5_decoder
Definition: imm5.c:177
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
attributes.h
imm5_close
static av_cold int imm5_close(AVCodecContext *avctx)
Definition: imm5.c:167
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:704
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:676
AVCodecContext::height
int height
Definition: avcodec.h:592
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:631
avcodec.h
ret
ret
Definition: filter_design.txt:187
avcodec_flush_buffers
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal codec state / flush internal buffers.
Definition: avcodec.c:380
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVCodecContext
main external API structure.
Definition: avcodec.h:431
av_packet_make_writable
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:515
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:607
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
AVPacket
This structure stores compressed data.
Definition: packet.h:535
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:592
imm5_init
static av_cold int imm5_init(AVCodecContext *avctx)
Definition: imm5.c:53
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27