sndio_dec.c
Go to the documentation of this file.
1 /*
2  * sndio play and grab interface
3  * Copyright (c) 2010 Jacob Meuser
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdint.h>
23 #include <sndio.h>
24 
25 #include "libavformat/avformat.h"
26 #include "libavformat/internal.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/time.h"
29 
30 #include "sndio_common.h"
31 
33 {
34  SndioData *s = s1->priv_data;
35  AVStream *st;
36  int ret;
37 
38  st = avformat_new_stream(s1, NULL);
39  if (!st)
40  return AVERROR(ENOMEM);
41 
42  ret = ff_sndio_open(s1, 0, s1->filename);
43  if (ret < 0)
44  return ret;
45 
46  /* take real parameters */
48  st->codec->codec_id = s->codec_id;
49  st->codec->sample_rate = s->sample_rate;
50  st->codec->channels = s->channels;
51 
52  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
53 
54  return 0;
55 }
56 
58 {
59  SndioData *s = s1->priv_data;
60  int64_t bdelay, cur_time;
61  int ret;
62 
63  if ((ret = av_new_packet(pkt, s->buffer_size)) < 0)
64  return ret;
65 
66  ret = sio_read(s->hdl, pkt->data, pkt->size);
67  if (ret == 0 || sio_eof(s->hdl)) {
68  av_free_packet(pkt);
69  return AVERROR_EOF;
70  }
71 
72  pkt->size = ret;
73  s->softpos += ret;
74 
75  /* compute pts of the start of the packet */
76  cur_time = av_gettime();
77 
78  bdelay = ret + s->hwpos - s->softpos;
79 
80  /* convert to pts */
81  pkt->pts = cur_time - ((bdelay * 1000000) /
82  (s->bps * s->channels * s->sample_rate));
83 
84  return 0;
85 }
86 
88 {
89  SndioData *s = s1->priv_data;
90 
91  ff_sndio_close(s);
92 
93  return 0;
94 }
95 
96 static const AVOption options[] = {
97  { "sample_rate", "", offsetof(SndioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
98  { "channels", "", offsetof(SndioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
99  { NULL },
100 };
101 
102 static const AVClass sndio_demuxer_class = {
103  .class_name = "sndio indev",
104  .item_name = av_default_item_name,
105  .option = options,
106  .version = LIBAVUTIL_VERSION_INT,
107 };
108 
110  .name = "sndio",
111  .long_name = NULL_IF_CONFIG_SMALL("sndio audio capture"),
112  .priv_data_size = sizeof(SndioData),
116  .flags = AVFMT_NOFILE,
117  .priv_class = &sndio_demuxer_class,
118 };
const char * s
Definition: avisynth_c.h:668
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
AVOption.
Definition: opt.h:251
static const AVOption options[]
Definition: sndio_dec.c:96
int channels
Definition: sndio_common.h:41
av_default_item_name
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
struct SndioData SndioData
static int64_t cur_time
Definition: ffserver.c:325
int ff_sndio_close(SndioData *s)
Definition: sndio_common.c:112
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: sndio_dec.c:57
Format I/O context.
Definition: avformat.h:944
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
enum AVCodecID codec_id
Definition: sndio_common.h:34
#define av_cold
Definition: attributes.h:78
AVOptions.
int sample_rate
Definition: sndio_common.h:42
static AVPacket pkt
Definition: demuxing.c:56
static const AVClass sndio_demuxer_class
Definition: sndio_dec.c:102
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint8_t * data
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:73
AVInputFormat ff_sndio_demuxer
Definition: sndio_dec.c:109
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
char filename[1024]
input or output filename
Definition: avformat.h:994
ret
Definition: avfilter.c:821
static av_cold int audio_read_close(AVFormatContext *s1)
Definition: sndio_dec.c:87
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
sample_rate
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
int buffer_size
Definition: sndio_common.h:39
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
Describe the class of an AVClass context structure.
Definition: log.h:50
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:282
#define s1
Definition: regdef.h:38
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
static int flags
Definition: cpu.c:23
int64_t hwpos
Definition: sndio_common.h:35
int64_t softpos
Definition: sndio_common.h:36
struct sio_hdl * hdl
Definition: sndio_common.h:33
Main libavformat public API header.
av_cold int ff_sndio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: sndio_common.c:36
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:345
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
static av_cold int audio_read_header(AVFormatContext *s1)
Definition: sndio_dec.c:32
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...