oss_dec.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "config.h"
  22. #include <stdint.h>
  23. #if HAVE_SOUNDCARD_H
  24. #include <soundcard.h>
  25. #else
  26. #include <sys/soundcard.h>
  27. #endif
  28. #if HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #include <fcntl.h>
  32. #include <sys/ioctl.h>
  33. #include "libavutil/internal.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/time.h"
  36. #include "libavcodec/avcodec.h"
  37. #include "avdevice.h"
  38. #include "libavformat/internal.h"
  39. #include "oss.h"
  40. static int audio_read_header(AVFormatContext *s1)
  41. {
  42. OSSAudioData *s = s1->priv_data;
  43. AVStream *st;
  44. int ret;
  45. st = avformat_new_stream(s1, NULL);
  46. if (!st) {
  47. return AVERROR(ENOMEM);
  48. }
  49. ret = ff_oss_audio_open(s1, 0, s1->filename);
  50. if (ret < 0) {
  51. return AVERROR(EIO);
  52. }
  53. /* take real parameters */
  54. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  55. st->codecpar->codec_id = s->codec_id;
  56. st->codecpar->sample_rate = s->sample_rate;
  57. st->codecpar->channels = s->channels;
  58. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  59. return 0;
  60. }
  61. static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
  62. {
  63. OSSAudioData *s = s1->priv_data;
  64. int ret, bdelay;
  65. int64_t cur_time;
  66. struct audio_buf_info abufi;
  67. if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
  68. return ret;
  69. ret = read(s->fd, pkt->data, pkt->size);
  70. if (ret <= 0){
  71. av_packet_unref(pkt);
  72. pkt->size = 0;
  73. if (ret<0) return AVERROR(errno);
  74. else return AVERROR_EOF;
  75. }
  76. pkt->size = ret;
  77. /* compute pts of the start of the packet */
  78. cur_time = av_gettime();
  79. bdelay = ret;
  80. if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
  81. bdelay += abufi.bytes;
  82. }
  83. /* subtract time represented by the number of bytes in the audio fifo */
  84. cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
  85. /* convert to wanted units */
  86. pkt->pts = cur_time;
  87. if (s->flip_left && s->channels == 2) {
  88. int i;
  89. short *p = (short *) pkt->data;
  90. for (i = 0; i < ret; i += 4) {
  91. *p = ~*p;
  92. p += 2;
  93. }
  94. }
  95. return 0;
  96. }
  97. static int audio_read_close(AVFormatContext *s1)
  98. {
  99. OSSAudioData *s = s1->priv_data;
  100. ff_oss_audio_close(s);
  101. return 0;
  102. }
  103. static const AVOption options[] = {
  104. { "sample_rate", "", offsetof(OSSAudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  105. { "channels", "", offsetof(OSSAudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  106. { NULL },
  107. };
  108. static const AVClass oss_demuxer_class = {
  109. .class_name = "OSS demuxer",
  110. .item_name = av_default_item_name,
  111. .option = options,
  112. .version = LIBAVUTIL_VERSION_INT,
  113. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
  114. };
  115. AVInputFormat ff_oss_demuxer = {
  116. .name = "oss",
  117. .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
  118. .priv_data_size = sizeof(OSSAudioData),
  119. .read_header = audio_read_header,
  120. .read_packet = audio_read_packet,
  121. .read_close = audio_read_close,
  122. .flags = AVFMT_NOFILE,
  123. .priv_class = &oss_demuxer_class,
  124. };