FFmpeg
Loading...
Searching...
No Matches
parse_skip_samples.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 * Feed packets of several AC-3 frames, with skip samples side data on the
21 * first one or two, through the full parser and decode the split packets.
22 */
23
24#include <inttypes.h>
25#include <stdio.h>
26#include <string.h>
27
28#include "libavutil/file.h"
30#include "libavutil/log.h"
31#include "libavcodec/avcodec.h"
33#include "libavformat/demux.h"
35
36#define FRAME_SIZE 1536 /* bytes per frame of the sample */
37#define FRAMES_PER_PACKET 4
38
39static uint8_t *file_buf;
40static size_t file_size, file_pos;
42static uint8_t reason_start, reason_end;
43
45{
47
48 if (!st)
49 return AVERROR(ENOMEM);
52 st->codecpar->sample_rate = 48000;
53 avpriv_set_pts_info(st, 64, 1, 48000);
55 return 0;
56}
57
59{
61 int ret;
62
63 if (!size)
64 return AVERROR_EOF;
66 if (ret < 0)
67 return ret;
68 memcpy(pkt->data, file_buf + file_pos, size);
71 if (!sd)
72 return AVERROR(ENOMEM);
74 AV_WL32(sd + 4, skip_end);
75 sd[8] = reason_start;
76 sd[9] = reason_end;
77 }
78 file_pos += size;
79 return 0;
80}
81
83 .p.name = "test",
84 .p.flags = AVFMT_NOFILE,
85 .read_header = read_header,
86 .read_packet = read_packet,
87};
88
89static int run(void)
90{
93 AVCodecContext *avctx = NULL;
96 int64_t samples = 0;
97 int ret, n = 0;
98
99 file_pos = 0;
100 if (!pkt || !frame || !codec) {
101 ret = AVERROR(ENOMEM);
102 goto end;
103 }
105 if (ret < 0)
106 goto end;
107 avctx = avcodec_alloc_context3(codec);
108 if (!avctx) {
109 ret = AVERROR(ENOMEM);
110 goto end;
111 }
112 ret = avcodec_open2(avctx, codec, NULL);
113 if (ret < 0)
114 goto end;
115
116 printf("skip %"PRIu32"/%"PRIu32" reason %d/%d\n",
118 while ((ret = av_read_frame(s, pkt)) >= 0) {
119 size_t sd_size;
121 &sd_size);
122 /* Check trailing discard at the end of the group and leading skip everywhere. */
123 if (sd && sd_size >= 10 && (!skip_end || n == 0 || n >= FRAMES_PER_PACKET - 1 ||
124 AV_RL32(sd) || sd[8]))
125 printf("packet %d: pts %"PRId64" size %d skip %"PRIu32"/%"PRIu32
126 " reason %d/%d\n", n, pkt->pts, pkt->size,
127 AV_RL32(sd), AV_RL32(sd + 4), sd[8], sd[9]);
128 n++;
129 ret = avcodec_send_packet(avctx, pkt);
131 if (ret < 0)
132 goto end;
133 while ((ret = avcodec_receive_frame(avctx, frame)) >= 0) {
134 samples += frame->nb_samples;
136 }
137 if (ret != AVERROR(EAGAIN))
138 goto end;
139 }
140 if (ret != AVERROR_EOF)
141 goto end;
142 ret = avcodec_send_packet(avctx, NULL);
143 while (ret >= 0 && (ret = avcodec_receive_frame(avctx, frame)) >= 0) {
144 samples += frame->nb_samples;
146 }
147 if (!skip_end)
148 printf("packets %d, decoded samples %"PRId64"\n", n, samples);
149 ret = ret == AVERROR_EOF ? 0 : ret;
150
151end:
152 avcodec_free_context(&avctx);
156 return ret;
157}
158
159int main(int argc, char **argv)
160{
161 static const struct {
162 uint32_t start, end;
163 uint8_t reason_start, reason_end;
164 uint32_t next_start;
165 } tests[] = {
166 { 2000, 0, 0, 0, 0 }, /* leading skip ending in the second frame */
167 { 3500, 0, 0, 0, 0 }, /* leading skip over three frames */
168 { 1000, 0, 0, 0, 1100 }, /* successive packets with distinct leading skips */
169 { 3500, 100, 1, 1, 0 }, /* leading skip and trailing discard */
170 };
171 int ret = 0;
172
173 if (argc < 2) {
174 fprintf(stderr, "Usage: %s <ac3 file>\n", argv[0]);
175 return 1;
176 }
178 if (av_file_map(argv[1], &file_buf, &file_size, 0, NULL) < 0)
179 return 1;
180 for (int i = 0; i < FF_ARRAY_ELEMS(tests) && ret >= 0; i++) {
181 skip_start = tests[i].start;
182 skip_end = tests[i].end;
183 reason_start = tests[i].reason_start;
184 reason_end = tests[i].reason_end;
185 next_skip_start = tests[i].next_start;
186 ret = run();
187 }
189 return ret < 0;
190}
Libavcodec external API header.
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:865
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition avformat.h:490
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition avformat.h:613
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FRAMES_PER_PACKET
Definition codec2.c:261
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
__device__ int printf(const char *,...)
static AVPacket * pkt
static AVFrame * frame
static const FFInputFormat test_demuxer
Definition demux_probe.c:69
int main
Definition dovi_rpuenc.c:38
int ret
Definition ffplay.c:391
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:580
const TestCase tests[]
Definition fifo_muxer.c:363
Misc file utilities.
#define FRAME_SIZE
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition avcodec.c:142
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition options.c:149
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition allcodecs.c:999
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_AC3
Definition codec_id.h:462
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Alias for avcodec_receive_frame_flags(avctx, frame, 0).
Definition avcodec.c:718
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition decode.c:738
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1662
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition demux.c:231
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition demux.c:400
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:499
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void av_log_set_level(int level)
Set the log level.
Definition log.c:482
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_WL32(p, v)
#define AV_RL32(p)
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:392
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition file.c:175
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition file.c:114
#define FFMIN(a, b)
Definition macros.h:49
static uint32_t skip_end
static size_t file_size
static size_t file_pos
static uint8_t * file_buf
static uint8_t reason_start
static int read_packet(AVFormatContext *s, AVPacket *pkt)
static uint8_t reason_end
static int run(void)
static int read_header(AVFormatContext *s)
static uint32_t skip_start
static uint32_t next_skip_start
#define FF_ARRAY_ELEMS(a)
main external API structure.
Definition avcodec.h:443
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
AVCodec.
Definition codec.h:175
Format I/O context.
Definition avformat.h:1401
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
enum AVStreamParseType need_parsing
Definition internal.h:346
int size