FFmpeg: libavdevice/oss_dec.c Source File

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 "libavcodec/avcodec.h"
38 
39 #include "avdevice.h"
40 #include "libavformat/internal.h"
41 
42 #include "oss.h"
43 
44  static int audio_read_header(AVFormatContext *s1)
45 {
46  OSSAudioData *s = s1->priv_data;
47  AVStream *st;
48  int ret;
49 
50  st = avformat_new_stream(s1, NULL);
51  if (!st) {
52  return AVERROR(ENOMEM);
53  }
54 
55  ret = ff_oss_audio_open(s1, 0, s1->url);
56  if (ret < 0) {
57  return AVERROR(EIO);
58  }
59 
60  /* take real parameters */
61  st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
62  st->codecpar->codec_id = s->codec_id;
63  st->codecpar->sample_rate = s->sample_rate;
64  st->codecpar->channels = s->channels;
65 
66  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
67  return 0;
68 }
69 
70  static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
71 {
72  OSSAudioData *s = s1->priv_data;
73  int ret, bdelay;
74  int64_t cur_time;
75  struct audio_buf_info abufi;
76 
77  if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
78  return ret;
79 
80  ret = read(s->fd, pkt->data, pkt->size);
81  if (ret <= 0){
82  av_packet_unref(pkt);
83  pkt->size = 0;
84  if (ret<0) return AVERROR(errno);
85  else return AVERROR_EOF;
86  }
87  pkt->size = ret;
88 
89  /* compute pts of the start of the packet */
90  cur_time = av_gettime();
91  bdelay = ret;
92  if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
93  bdelay += abufi.bytes;
94  }
95  /* subtract time represented by the number of bytes in the audio fifo */
96  cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
97 
98  /* convert to wanted units */
99  pkt->pts = cur_time;
100 
101  if (s->flip_left && s->channels == 2) {
102  int i;
103  short *p = (short *) pkt->data;
104 
105  for (i = 0; i < ret; i += 4) {
106  *p = ~*p;
107  p += 2;
108  }
109  }
110  return 0;
111 }
112 
113  static int audio_read_close(AVFormatContext *s1)
114 {
115  OSSAudioData *s = s1->priv_data;
116 
117  ff_oss_audio_close(s);
118  return 0;
119 }
120 
121  static const AVOption options[] = {
122  { "sample_rate", "", offsetof(OSSAudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
123  { "channels", "", offsetof(OSSAudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
124  { NULL },
125 };
126 
127  static const AVClass oss_demuxer_class = {
128  .class_name = "OSS demuxer",
129  .item_name = av_default_item_name,
130  .option = options,
131  .version = LIBAVUTIL_VERSION_INT,
132  .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
133 };
134 
135  AVInputFormat ff_oss_demuxer = {
136  .name = "oss",
137  .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
138  .priv_data_size = sizeof(OSSAudioData),
139  .read_header = audio_read_header,
140  .read_packet = audio_read_packet,
141  .read_close = audio_read_close,
142  .flags = AVFMT_NOFILE,
143  .priv_class = &oss_demuxer_class,
144 };
NULL
#define NULL
Definition: coverity.c:32
s
const char * s
Definition: avisynth_c.h:768
AVOption
AVOption.
Definition: opt.h:246
OSSAudioData
Definition: oss.h:28
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
avpriv_set_pts_info
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:4820
options
static const AVOption options[]
Definition: oss_dec.c:121
channels
channels
Definition: aptx.c:30
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3884
AVPacket::size
int size
Definition: avcodec.h:1431
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
audio_read_header
static int audio_read_header(AVFormatContext *s1)
Definition: oss_dec.c:44
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
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:72
opt.h
AVOptions.
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4450
OSSAudioData::channels
int channels
Definition: oss.h:32
AVPacket::data
uint8_t * data
Definition: avcodec.h:1430
flags
static int flags
Definition: log.c:55
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
OSSAudioData::codec_id
enum AVCodecID codec_id
Definition: oss.h:34
ff_oss_audio_open
int ff_oss_audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Definition: oss.c:40
avdevice.h
Main libavdevice API header.
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:86
AVERROR
#define AVERROR(e)
Definition: error.h:43
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:186
AVFormatContext::url
char * url
input or output URL.
Definition: avformat.h:1438
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3880
audio_read_close
static int audio_read_close(AVFormatContext *s1)
Definition: oss_dec.c:113
audio_read_packet
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: oss_dec.c:70
internal.h
common internal API header
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
AVStream
Stream structure.
Definition: avformat.h:873
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
oss_demuxer_class
static const AVClass oss_demuxer_class
Definition: oss_dec.c:127
sample_rate
sample_rate
Definition: ffmpeg_filter.c:190
avcodec.h
Libavcodec external API header.
OSSAudioData::fd
int fd
Definition: oss.h:30
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:592
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
OSSAudioData::frame_size
int frame_size
Definition: oss.h:33
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:277
s1
#define s1
Definition: regdef.h:38
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: avcodec.h:3994
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:465
OSSAudioData::flip_left
unsigned int flip_left
Definition: oss.h:35
ff_oss_audio_close
int ff_oss_audio_close(OSSAudioData *s)
Definition: oss.c:135
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1370
AVCodecParameters::channels
int channels
Audio only.
Definition: avcodec.h:3990
OSSAudioData::sample_rate
int sample_rate
Definition: oss.h:31
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1407
ff_oss_demuxer
AVInputFormat ff_oss_demuxer
Definition: oss_dec.c:135
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1423
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469

Generated on Sun May 13 2018 02:03:59 for FFmpeg by   doxygen 1.8.6

AltStyle によって変換されたページ (->オリジナル) /