FFmpeg
oss_dec.c
Go to the documentation of this file.
1 /*
2  * Linux audio play interface
3  * Copyright (c) 2000, 2001 Fabrice Bellard
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 "config.h"
23 
24 #include <stdint.h>
25 
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <sys/soundcard.h>
32 
33 #include "libavutil/internal.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/time.h"
36 
37 #include "avdevice.h"
38 #include "libavformat/internal.h"
39 
40 #include "oss.h"
41 
43 {
44  OSSAudioData *s = s1->priv_data;
45  AVStream *st;
46  int ret;
47 
49  if (!st) {
50  return AVERROR(ENOMEM);
51  }
52 
53  ret = ff_oss_audio_open(s1, 0, s1->url);
54  if (ret < 0) {
55  return AVERROR(EIO);
56  }
57 
58  /* take real parameters */
60  st->codecpar->codec_id = s->codec_id;
61  st->codecpar->sample_rate = s->sample_rate;
62  st->codecpar->ch_layout.nb_channels = s->channels;
63 
64  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
65  return 0;
66 }
67 
69 {
70  OSSAudioData *s = s1->priv_data;
71  int ret, bdelay;
72  int64_t cur_time;
73  struct audio_buf_info abufi;
74 
75  if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
76  return ret;
77 
78  ret = read(s->fd, pkt->data, pkt->size);
79  if (ret <= 0){
81  pkt->size = 0;
82  if (ret<0) return AVERROR(errno);
83  else return AVERROR_EOF;
84  }
85  pkt->size = ret;
86 
87  /* compute pts of the start of the packet */
88  cur_time = av_gettime();
89  bdelay = ret;
90  if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
91  bdelay += abufi.bytes;
92  }
93  /* subtract time represented by the number of bytes in the audio fifo */
94  cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->sample_size * s->channels);
95 
96  /* convert to wanted units */
97  pkt->pts = cur_time;
98 
99  if (s->flip_left && s->channels == 2) {
100  int i;
101  short *p = (short *) pkt->data;
102 
103  for (i = 0; i < ret; i += 4) {
104  *p = ~*p;
105  p += 2;
106  }
107  }
108  return 0;
109 }
110 
112 {
113  OSSAudioData *s = s1->priv_data;
114 
116  return 0;
117 }
118 
119 static const AVOption options[] = {
120  { "sample_rate", "", offsetof(OSSAudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
121  { "channels", "", offsetof(OSSAudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
122  { NULL },
123 };
124 
125 static const AVClass oss_demuxer_class = {
126  .class_name = "OSS indev",
127  .item_name = av_default_item_name,
128  .option = options,
129  .version = LIBAVUTIL_VERSION_INT,
131 };
132 
134  .name = "oss",
135  .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
136  .priv_data_size = sizeof(OSSAudioData),
140  .flags = AVFMT_NOFILE,
141  .priv_class = &oss_demuxer_class,
142 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:423
ff_oss_audio_open
int ff_oss_audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: oss.c:40
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
opt.h
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVPacket::data
uint8_t * data
Definition: packet.h:491
AVOption
AVOption.
Definition: opt.h:251
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:317
sample_rate
sample_rate
Definition: ffmpeg_filter.c:368
ff_oss_audio_close
int ff_oss_audio_close(OSSAudioData *s)
Definition: oss.c:137
avpriv_set_pts_info
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:761
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
Definition: log.h:43
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:549
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:98
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:554
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
s1
#define s1
Definition: regdef.h:38
channels
channels
Definition: aptx.h:31
AVFormatContext
Format I/O context.
Definition: avformat.h:1115
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:864
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
audio_read_close
static int audio_read_close(AVFormatContext *s1)
Definition: oss_dec.c:111
oss_demuxer_class
static const AVClass oss_demuxer_class
Definition: oss_dec.c:125
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
OSSAudioData
Definition: oss.h:29
time.h
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:206
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:171
AVPacket::size
int size
Definition: packet.h:492
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
ff_oss_demuxer
const AVInputFormat ff_oss_demuxer
Definition: oss_dec.c:133
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:469
avdevice.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:484
internal.h
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:282
options
static const AVOption options[]
Definition: oss_dec.c:119
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:841
AVClass::class_name
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:71
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
audio_read_header
static int audio_read_header(AVFormatContext *s1)
Definition: oss_dec.c:42
audio_read_packet
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: oss_dec.c:68
oss.h
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:468
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
read
static uint32_t BS_FUNC() read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
Definition: bitstream_template.h:231