You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.3KB

  1. /*
  2. * PCM common functions
  3. * Copyright (c) 2003 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 "libavutil/mathematics.h"
  22. #include "avformat.h"
  23. #include "pcm.h"
  24. #define RAW_SAMPLES 1024
  25. int ff_pcm_read_packet(AVFormatContext *s, AVPacket *pkt)
  26. {
  27. int ret, size;
  28. size= RAW_SAMPLES*s->streams[0]->codec->block_align;
  29. if (size <= 0)
  30. return AVERROR(EINVAL);
  31. ret= av_get_packet(s->pb, pkt, size);
  32. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  33. pkt->stream_index = 0;
  34. return ret;
  35. }
  36. int ff_pcm_read_seek(AVFormatContext *s,
  37. int stream_index, int64_t timestamp, int flags)
  38. {
  39. AVStream *st;
  40. int block_align, byte_rate;
  41. int64_t pos, ret;
  42. st = s->streams[0];
  43. block_align = st->codec->block_align ? st->codec->block_align :
  44. (av_get_bits_per_sample(st->codec->codec_id) * st->codec->channels) >> 3;
  45. byte_rate = st->codec->bit_rate ? st->codec->bit_rate >> 3 :
  46. block_align * st->codec->sample_rate;
  47. if (block_align <= 0 || byte_rate <= 0)
  48. return -1;
  49. if (timestamp < 0) timestamp = 0;
  50. /* compute the position by aligning it to block_align */
  51. pos = av_rescale_rnd(timestamp * byte_rate,
  52. st->time_base.num,
  53. st->time_base.den * (int64_t)block_align,
  54. (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
  55. pos *= block_align;
  56. /* recompute exact position */
  57. st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
  58. if ((ret = avio_seek(s->pb, pos + s->data_offset, SEEK_SET)) < 0)
  59. return ret;
  60. return 0;
  61. }