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.

208 lines
6.8KB

  1. /*
  2. * Sony OpenMG (OMA) demuxer
  3. *
  4. * Copyright (c) 2008 Maxim Poliakovski
  5. * 2008 Benjamin Larsson
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file libavformat/oma.c
  25. * This is a demuxer for Sony OpenMG Music files
  26. *
  27. * Known file extensions: ".oma", "aa3"
  28. * The format of such files consists of three parts:
  29. * - "ea3" header carrying overall info and metadata.
  30. * - "EA3" header is a Sony-specific header containing information about
  31. * the OpenMG file: codec type (usually ATRAC, can also be MP3 or WMA),
  32. * codec specific info (packet size, sample rate, channels and so on)
  33. * and DRM related info (file encryption, content id).
  34. * - Sound data organized in packets follow the EA3 header
  35. * (can be encrypted using the Sony DRM!).
  36. *
  37. * LIMITATIONS: This version supports only plain (unencrypted) OMA files.
  38. * If any DRM-protected (encrypted) file is encountered you will get the
  39. * corresponding error message. Try to remove the encryption using any
  40. * Sony software (for example SonicStage).
  41. * CODEC SUPPORT: Only ATRAC3 codec is currently supported!
  42. */
  43. #include "avformat.h"
  44. #include "libavutil/intreadwrite.h"
  45. #include "raw.h"
  46. #include "riff.h"
  47. #define EA3_HEADER_SIZE 96
  48. enum {
  49. OMA_CODECID_ATRAC3 = 0,
  50. OMA_CODECID_ATRAC3P = 1,
  51. OMA_CODECID_MP3 = 3,
  52. OMA_CODECID_LPCM = 4,
  53. OMA_CODECID_WMA = 5,
  54. };
  55. static const AVCodecTag codec_oma_tags[] = {
  56. { CODEC_ID_ATRAC3, OMA_CODECID_ATRAC3 },
  57. { CODEC_ID_ATRAC3P, OMA_CODECID_ATRAC3P },
  58. { CODEC_ID_MP3, OMA_CODECID_MP3 },
  59. };
  60. static int oma_read_header(AVFormatContext *s,
  61. AVFormatParameters *ap)
  62. {
  63. static const uint16_t srate_tab[6] = {320,441,480,882,960,0};
  64. int ret, ea3_taglen, EA3_pos, framesize, jsflag, samplerate;
  65. uint32_t codec_params;
  66. int16_t eid;
  67. uint8_t buf[EA3_HEADER_SIZE];
  68. uint8_t *edata;
  69. AVStream *st;
  70. ret = get_buffer(s->pb, buf, 10);
  71. if (ret != 10)
  72. return -1;
  73. if(!memcmp(buf, "ea3", 3)) {
  74. ea3_taglen = ((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) | ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f);
  75. EA3_pos = ea3_taglen + 10;
  76. if (buf[5] & 0x10)
  77. EA3_pos += 10;
  78. url_fseek(s->pb, EA3_pos, SEEK_SET);
  79. ret = get_buffer(s->pb, buf, EA3_HEADER_SIZE);
  80. if (ret != EA3_HEADER_SIZE)
  81. return -1;
  82. } else {
  83. ret = get_buffer(s->pb, buf + 10, EA3_HEADER_SIZE - 10);
  84. EA3_pos = 0;
  85. }
  86. if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}),3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) {
  87. av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n");
  88. return -1;
  89. }
  90. eid = AV_RB16(&buf[6]);
  91. if (eid != -1 && eid != -128) {
  92. av_log(s, AV_LOG_ERROR, "Encrypted file! Eid: %d\n", eid);
  93. return -1;
  94. }
  95. codec_params = AV_RB24(&buf[33]);
  96. st = av_new_stream(s, 0);
  97. if (!st)
  98. return AVERROR(ENOMEM);
  99. st->start_time = 0;
  100. st->codec->codec_type = CODEC_TYPE_AUDIO;
  101. st->codec->codec_tag = buf[32];
  102. st->codec->codec_id = ff_codec_get_id(codec_oma_tags, st->codec->codec_tag);
  103. switch (buf[32]) {
  104. case OMA_CODECID_ATRAC3:
  105. samplerate = srate_tab[(codec_params >> 13) & 7]*100;
  106. if (samplerate != 44100)
  107. av_log(s, AV_LOG_ERROR, "Unsupported sample rate, send sample file to developers: %d\n", samplerate);
  108. framesize = (codec_params & 0x3FF) * 8;
  109. jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */
  110. st->codec->channels = 2;
  111. st->codec->sample_rate = samplerate;
  112. st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
  113. /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */
  114. st->codec->extradata_size = 14;
  115. edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
  116. if (!edata)
  117. return AVERROR(ENOMEM);
  118. st->codec->extradata = edata;
  119. AV_WL16(&edata[0], 1); // always 1
  120. AV_WL32(&edata[2], samplerate); // samples rate
  121. AV_WL16(&edata[6], jsflag); // coding mode
  122. AV_WL16(&edata[8], jsflag); // coding mode
  123. AV_WL16(&edata[10], 1); // always 1
  124. // AV_WL16(&edata[12], 0); // always 0
  125. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  126. break;
  127. case OMA_CODECID_ATRAC3P:
  128. st->codec->channels = (codec_params >> 10) & 7;
  129. framesize = ((codec_params & 0x3FF) * 8) + 8;
  130. st->codec->sample_rate = srate_tab[(codec_params >> 13) & 7]*100;
  131. st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
  132. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  133. av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
  134. break;
  135. case OMA_CODECID_MP3:
  136. st->need_parsing = AVSTREAM_PARSE_FULL;
  137. framesize = 1024;
  138. break;
  139. default:
  140. av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
  141. return -1;
  142. break;
  143. }
  144. st->codec->block_align = framesize;
  145. url_fseek(s->pb, EA3_pos + EA3_HEADER_SIZE, SEEK_SET);
  146. return 0;
  147. }
  148. static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
  149. {
  150. int ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align);
  151. pkt->stream_index = 0;
  152. if (ret <= 0)
  153. return AVERROR(EIO);
  154. return ret;
  155. }
  156. static int oma_read_probe(AVProbeData *p)
  157. {
  158. if (!memcmp(p->buf, ((const uint8_t[]){'e', 'a', '3', 3, 0}), 5) ||
  159. (!memcmp(p->buf, "EA3", 3) &&
  160. !p->buf[4] && p->buf[5] == EA3_HEADER_SIZE))
  161. return AVPROBE_SCORE_MAX;
  162. else
  163. return 0;
  164. }
  165. AVInputFormat oma_demuxer = {
  166. "oma",
  167. NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
  168. 0,
  169. oma_read_probe,
  170. oma_read_header,
  171. oma_read_packet,
  172. 0,
  173. pcm_read_seek,
  174. .flags= AVFMT_GENERIC_INDEX,
  175. .extensions = "oma,aa3",
  176. .codec_tag= (const AVCodecTag* const []){codec_oma_tags, 0},
  177. };