FFmpeg
vp9_superframe_split_bsf.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * This bitstream filter splits VP9 superframes into packets containing
22  * just one frame.
23  */
24 
25 #include <stddef.h>
26 
27 #include "bsf.h"
28 #include "bsf_internal.h"
29 #include "bytestream.h"
30 #include "get_bits.h"
31 
32 typedef struct VP9SFSplitContext {
34 
35  int nb_frames;
38  int sizes[8];
40 
42 {
44  AVPacket *in;
45  int i, j, ret, marker;
46  int is_superframe = !!s->buffer_pkt->data;
47 
48  if (!s->buffer_pkt->data) {
49  ret = ff_bsf_get_packet_ref(ctx, s->buffer_pkt);
50  if (ret < 0)
51  return ret;
52  in = s->buffer_pkt;
53 
54  marker = in->data[in->size - 1];
55  if ((marker & 0xe0) == 0xc0) {
56  int length_size = 1 + ((marker >> 3) & 0x3);
57  int nb_frames = 1 + (marker & 0x7);
58  int idx_size = 2 + nb_frames * length_size;
59 
60  if (in->size >= idx_size && in->data[in->size - idx_size] == marker) {
61  GetByteContext bc;
62  int64_t total_size = 0;
63 
64  bytestream2_init(&bc, in->data + in->size + 1 - idx_size,
65  nb_frames * length_size);
66 
67  for (i = 0; i < nb_frames; i++) {
68  int frame_size = 0;
69  for (j = 0; j < length_size; j++)
70  frame_size |= bytestream2_get_byte(&bc) << (j * 8);
71 
72  total_size += frame_size;
73  if (frame_size < 0 || total_size > in->size - idx_size) {
75  "Invalid frame size in a superframe: %d\n", frame_size);
76  ret = AVERROR(EINVAL);
77  goto fail;
78  }
79  s->sizes[i] = frame_size;
80  }
81  s->nb_frames = nb_frames;
82  s->next_frame = 0;
83  s->next_frame_offset = 0;
84  is_superframe = 1;
85  }
86  }
87  }
88 
89  if (is_superframe) {
90  GetBitContext gb;
91  int profile, invisible = 0;
92 
93  ret = av_packet_ref(out, s->buffer_pkt);
94  if (ret < 0)
95  goto fail;
96 
97  out->data += s->next_frame_offset;
98  out->size = s->sizes[s->next_frame];
99 
100  s->next_frame_offset += out->size;
101  s->next_frame++;
102 
103  if (s->next_frame >= s->nb_frames)
104  av_packet_unref(s->buffer_pkt);
105 
106  ret = init_get_bits8(&gb, out->data, out->size);
107  if (ret < 0)
108  goto fail;
109 
110  get_bits(&gb, 2); // frame_marker
111  profile = get_bits1(&gb);
112  profile |= get_bits1(&gb) << 1;
113  if (profile == 3)
114  get_bits1(&gb);
115  if (!get_bits1(&gb)) {
116  get_bits1(&gb);
117  invisible = !get_bits1(&gb);
118  }
119 
120  if (invisible)
121  out->pts = AV_NOPTS_VALUE;
122 
123  } else {
124  av_packet_move_ref(out, s->buffer_pkt);
125  }
126 
127  return 0;
128 fail:
129  if (ret < 0)
131  av_packet_unref(s->buffer_pkt);
132  return ret;
133 }
134 
136 {
138 
139  s->buffer_pkt = av_packet_alloc();
140  if (!s->buffer_pkt)
141  return AVERROR(ENOMEM);
142 
143  return 0;
144 }
145 
147 {
149  av_packet_unref(s->buffer_pkt);
150 }
151 
153 {
155  av_packet_free(&s->buffer_pkt);
156 }
157 
159  .name = "vp9_superframe_split",
160  .priv_data_size = sizeof(VP9SFSplitContext),
165  .codec_ids = (const enum AVCodecID []){ AV_CODEC_ID_VP9, AV_CODEC_ID_NONE },
166 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:417
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
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
bsf_internal.h
out
FILE * out
Definition: movenc.c:54
GetByteContext
Definition: bytestream.h:33
AVBitStreamFilter::name
const char * name
Definition: bsf.h:91
profile
mfxU16 profile
Definition: qsvenc.c:45
AVPacket::data
uint8_t * data
Definition: packet.h:373
VP9SFSplitContext
Definition: vp9_superframe_split_bsf.c:32
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
AVBSFContext
The bitstream filter state.
Definition: bsf.h:47
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:380
bsf.h
fail
#define fail()
Definition: checkasm.h:127
GetBitContext
Definition: get_bits.h:62
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:678
s
#define s(width, name)
Definition: cbs_vp9.c:257
vp9_superframe_split_init
static int vp9_superframe_split_init(AVBSFContext *ctx)
Definition: vp9_superframe_split_bsf.c:135
frame_size
int frame_size
Definition: mxfenc.c:2207
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:218
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
VP9SFSplitContext::buffer_pkt
AVPacket * buffer_pkt
Definition: vp9_superframe_split_bsf.c:33
VP9SFSplitContext::sizes
int sizes[8]
Definition: vp9_superframe_split_bsf.c:38
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:593
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:499
av_packet_ref
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:425
av_packet_move_ref
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:474
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
VP9SFSplitContext::next_frame
int next_frame
Definition: vp9_superframe_split_bsf.c:36
AVPacket::size
int size
Definition: packet.h:374
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
VP9SFSplitContext::nb_frames
int nb_frames
Definition: vp9_superframe_split_bsf.c:35
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
VP9SFSplitContext::next_frame_offset
size_t next_frame_offset
Definition: vp9_superframe_split_bsf.c:37
ff_vp9_superframe_split_bsf
const AVBitStreamFilter ff_vp9_superframe_split_bsf
Definition: vp9_superframe_split_bsf.c:158
i
int i
Definition: input.c:406
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
ret
ret
Definition: filter_design.txt:187
vp9_superframe_split_flush
static void vp9_superframe_split_flush(AVBSFContext *ctx)
Definition: vp9_superframe_split_bsf.c:146
AVBitStreamFilter
Definition: bsf.h:90
vp9_superframe_split_filter
static int vp9_superframe_split_filter(AVBSFContext *ctx, AVPacket *out)
Definition: vp9_superframe_split_bsf.c:41
codec_ids
static enum AVCodecID codec_ids[]
Definition: aac_adtstoasc_bsf.c:148
AVPacket
This structure stores compressed data.
Definition: packet.h:350
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_bsf_get_packet_ref
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:252
vp9_superframe_split_uninit
static void vp9_superframe_split_uninit(AVBSFContext *ctx)
Definition: vp9_superframe_split_bsf.c:152
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1125