Libav
oss_audio_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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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_SOUNDCARD_H
27 #include <soundcard.h>
28 #else
29 #include <sys/soundcard.h>
30 #endif
31 
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/ioctl.h>
35 
36 #include "libavutil/internal.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/time.h"
39 
40 #include "libavcodec/avcodec.h"
41 
42 #include "libavformat/avformat.h"
43 #include "libavformat/internal.h"
44 
45 #include "oss_audio.h"
46 
48 {
49  OSSAudioData *s = s1->priv_data;
50  AVStream *st;
51  int ret;
52 
53  st = avformat_new_stream(s1, NULL);
54  if (!st) {
55  return AVERROR(ENOMEM);
56  }
57 
58  ret = ff_oss_audio_open(s1, 0, s1->filename);
59  if (ret < 0) {
60  return AVERROR(EIO);
61  }
62 
63  /* take real parameters */
65  st->codec->codec_id = s->codec_id;
66  st->codec->sample_rate = s->sample_rate;
67  st->codec->channels = s->channels;
68 
69  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
70  return 0;
71 }
72 
74 {
75  OSSAudioData *s = s1->priv_data;
76  int ret, bdelay;
77  int64_t cur_time;
78  struct audio_buf_info abufi;
79 
80  if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
81  return ret;
82 
83  ret = read(s->fd, pkt->data, pkt->size);
84  if (ret <= 0){
85  av_free_packet(pkt);
86  pkt->size = 0;
87  if (ret<0) return AVERROR(errno);
88  else return AVERROR_EOF;
89  }
90  pkt->size = ret;
91 
92  /* compute pts of the start of the packet */
93  cur_time = av_gettime();
94  bdelay = ret;
95  if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
96  bdelay += abufi.bytes;
97  }
98  /* subtract time represented by the number of bytes in the audio fifo */
99  cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
100 
101  /* convert to wanted units */
102  pkt->pts = cur_time;
103 
104  if (s->flip_left && s->channels == 2) {
105  int i;
106  short *p = (short *) pkt->data;
107 
108  for (i = 0; i < ret; i += 4) {
109  *p = ~*p;
110  p += 2;
111  }
112  }
113  return 0;
114 }
115 
117 {
118  OSSAudioData *s = s1->priv_data;
119 
121  return 0;
122 }
123 
124 static const AVOption options[] = {
125  { "sample_rate", "", offsetof(OSSAudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
126  { "channels", "", offsetof(OSSAudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
127  { NULL },
128 };
129 
130 static const AVClass oss_demuxer_class = {
131  .class_name = "OSS demuxer",
132  .item_name = av_default_item_name,
133  .option = options,
134  .version = LIBAVUTIL_VERSION_INT,
135 };
136 
138  .name = "oss",
139  .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
140  .priv_data_size = sizeof(OSSAudioData),
144  .flags = AVFMT_NOFILE,
145  .priv_class = &oss_demuxer_class,
146 };
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:243
AVOption.
Definition: opt.h:234
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
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.
Definition: utils.c:2821
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int size
Definition: avcodec.h:974
Format I/O context.
Definition: avformat.h:922
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:38
AVInputFormat ff_oss_demuxer
int channels
Definition: oss_audio.h:32
static int flags
Definition: log.c:44
#define AVERROR_EOF
End of file.
Definition: error.h:51
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:409
enum AVCodecID codec_id
Definition: oss_audio.h:34
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2518
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
void * priv_data
Format private data.
Definition: avformat.h:950
char filename[1024]
input or output filename
Definition: avformat.h:998
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
common internal API header
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:37
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: oss_audio_dec.c:73
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
static const AVClass oss_demuxer_class
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1791
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int ff_oss_audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: oss_audio.c:44
Describe the class of an AVClass context structure.
Definition: log.h:33
int frame_size
Definition: oss_audio.h:33
uint8_t * data
Definition: avcodec.h:973
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:265
unsigned int flip_left
Definition: oss_audio.h:35
int channels
number of audio channels
Definition: avcodec.h:1792
int ff_oss_audio_close(OSSAudioData *s)
Definition: oss_audio.c:132
int sample_rate
Definition: oss_audio.h:31
static int audio_read_close(AVFormatContext *s1)
static const AVOption options[]
av_default_item_name
Return the context name.
Definition: dnxhdenc.c:52
static int audio_read_header(AVFormatContext *s1)
Definition: oss_audio_dec.c:47
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
for(j=16;j >0;--j)