FFmpeg
Loading...
Searching...
No Matches
api-skip-samples-test.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 * Decode one packet carrying AV_PKT_DATA_SKIP_SAMPLES and print how many
21 * samples are output, for skips at the start and padding at the end of the
22 * frame, alone, together and overlapping.
23 */
24
25#include <stdio.h>
26#include <string.h>
27
28#include "libavcodec/avcodec.h"
31#include "libavutil/macros.h"
32
33#define FRAME_SAMPLES 1000
34
35static int decoded_samples(uint32_t skip, uint32_t discard)
36{
39 AVPacket *pkt = NULL;
41 uint8_t *side;
42 int nb_samples = 0;
43 int ret;
44
48 if (!ctx || !pkt || !frame) {
49 ret = AVERROR(ENOMEM);
50 goto end;
51 }
52
53 ctx->sample_rate = 48000;
54 ctx->pkt_timebase = (AVRational){ 1, 48000 };
55 av_channel_layout_default(&ctx->ch_layout, 1);
56 ret = avcodec_open2(ctx, codec, NULL);
57 if (ret < 0)
58 goto end;
59
61 if (ret < 0)
62 goto end;
63 memset(pkt->data, 0, pkt->size);
64 pkt->pts = 0;
66 if (!side) {
67 ret = AVERROR(ENOMEM);
68 goto end;
69 }
70 AV_WL32(side, skip);
71 AV_WL32(side + 4, discard);
72
74 if (ret < 0)
75 goto end;
77 if (ret < 0)
78 goto end;
79 while ((ret = avcodec_receive_frame(ctx, frame)) >= 0) {
80 nb_samples += frame->nb_samples;
82 }
83 ret = ret == AVERROR_EOF ? nb_samples : ret;
84
85end:
89 return ret;
90}
91
92int main(void)
93{
94 static const uint32_t cases[][2] = {
95 { 0, 0 },
96 { 300, 0 },
97 { 0, 400 },
98 { 300, 400 },
99 { 300, 700 },
100 { 300, 800 },
101 { 300, 1100 },
102 };
103
104 for (int i = 0; i < FF_ARRAY_ELEMS(cases); i++) {
105 int ret = decoded_samples(cases[i][0], cases[i][1]);
106 if (ret < 0) {
107 fprintf(stderr, "decoding failed: %s\n", av_err2str(ret));
108 return 1;
109 }
110 printf("frame %d skip %4"PRIu32" discard %4"PRIu32": %d samples\n",
111 FRAME_SAMPLES, cases[i][0], cases[i][1], ret);
112 }
113 return 0;
114}
static AVFormatContext * ctx
static int decoded_samples(uint32_t skip, uint32_t discard)
int main(void)
Libavcodec external API header.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
__device__ int printf(const char *,...)
static AVPacket * pkt
static AVFrame * frame
#define FRAME_SAMPLES
Definition dolby_e.h:27
int ret
Definition ffplay.c:391
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_PCM_S16LE
Definition codec_id.h:333
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
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
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
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#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_WL32(p, v)
Utility Preprocessor macros.
#define FF_ARRAY_ELEMS(a)
main external API structure.
Definition avcodec.h:443
AVCodec.
Definition codec.h:175
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
Rational number (pair of numerator and denominator).
Definition rational.h:58