oma.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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
  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. Except for starting with
  30. * "ea" instead of "ID", it's an ID3v2 header.
  31. * - "EA3" header is a Sony-specific header containing information about
  32. * the OpenMG file: codec type (usually ATRAC, can also be MP3 or WMA),
  33. * codec specific info (packet size, sample rate, channels and so on)
  34. * and DRM related info (file encryption, content id).
  35. * - Sound data organized in packets follow the EA3 header
  36. * (can be encrypted using the Sony DRM!).
  37. *
  38. * LIMITATIONS: This version supports only plain (unencrypted) OMA files.
  39. * If any DRM-protected (encrypted) file is encountered you will get the
  40. * corresponding error message. Try to remove the encryption using any
  41. * Sony software (for example SonicStage).
  42. * CODEC SUPPORT: Only ATRAC3 codec is currently supported!
  43. */
  44. #include "avformat.h"
  45. #include "libavutil/intreadwrite.h"
  46. #include "pcm.h"
  47. #include "riff.h"
  48. #include "id3v2.h"
  49. #define EA3_HEADER_SIZE 96
  50. enum {
  51. OMA_CODECID_ATRAC3 = 0,
  52. OMA_CODECID_ATRAC3P = 1,
  53. OMA_CODECID_MP3 = 3,
  54. OMA_CODECID_LPCM = 4,
  55. OMA_CODECID_WMA = 5,
  56. };
  57. static const AVCodecTag codec_oma_tags[] = {
  58. { CODEC_ID_ATRAC3, OMA_CODECID_ATRAC3 },
  59. { CODEC_ID_ATRAC3P, OMA_CODECID_ATRAC3P },
  60. { CODEC_ID_MP3, OMA_CODECID_MP3 },
  61. };
  62. #define ID3v2_EA3_MAGIC "ea3"
  63. static int oma_read_header(AVFormatContext *s,
  64. AVFormatParameters *ap)
  65. {
  66. static const uint16_t srate_tab[6] = {320,441,480,882,960,0};
  67. int ret, framesize, jsflag, samplerate;
  68. uint32_t codec_params;
  69. int16_t eid;
  70. uint8_t buf[EA3_HEADER_SIZE];
  71. uint8_t *edata;
  72. AVStream *st;
  73. ff_id3v2_read(s, ID3v2_EA3_MAGIC);
  74. ret = avio_read(s->pb, buf, EA3_HEADER_SIZE);
  75. if (ret < EA3_HEADER_SIZE)
  76. return -1;
  77. if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}),3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) {
  78. av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n");
  79. return -1;
  80. }
  81. eid = AV_RB16(&buf[6]);
  82. if (eid != -1 && eid != -128) {
  83. av_log(s, AV_LOG_ERROR, "Encrypted file! Eid: %d\n", eid);
  84. return -1;
  85. }
  86. codec_params = AV_RB24(&buf[33]);
  87. st = av_new_stream(s, 0);
  88. if (!st)
  89. return AVERROR(ENOMEM);
  90. st->start_time = 0;
  91. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  92. st->codec->codec_tag = buf[32];
  93. st->codec->codec_id = ff_codec_get_id(codec_oma_tags, st->codec->codec_tag);
  94. switch (buf[32]) {
  95. case OMA_CODECID_ATRAC3:
  96. samplerate = srate_tab[(codec_params >> 13) & 7]*100;
  97. if (samplerate != 44100)
  98. av_log_ask_for_sample(s, "Unsupported sample rate: %d\n",
  99. samplerate);
  100. framesize = (codec_params & 0x3FF) * 8;
  101. jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */
  102. st->codec->channels = 2;
  103. st->codec->sample_rate = samplerate;
  104. st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
  105. /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */
  106. st->codec->extradata_size = 14;
  107. edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
  108. if (!edata)
  109. return AVERROR(ENOMEM);
  110. st->codec->extradata = edata;
  111. AV_WL16(&edata[0], 1); // always 1
  112. AV_WL32(&edata[2], samplerate); // samples rate
  113. AV_WL16(&edata[6], jsflag); // coding mode
  114. AV_WL16(&edata[8], jsflag); // coding mode
  115. AV_WL16(&edata[10], 1); // always 1
  116. // AV_WL16(&edata[12], 0); // always 0
  117. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  118. break;
  119. case OMA_CODECID_ATRAC3P:
  120. st->codec->channels = (codec_params >> 10) & 7;
  121. framesize = ((codec_params & 0x3FF) * 8) + 8;
  122. st->codec->sample_rate = srate_tab[(codec_params >> 13) & 7]*100;
  123. st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
  124. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  125. av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
  126. break;
  127. case OMA_CODECID_MP3:
  128. st->need_parsing = AVSTREAM_PARSE_FULL;
  129. framesize = 1024;
  130. break;
  131. default:
  132. av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
  133. return -1;
  134. }
  135. st->codec->block_align = framesize;
  136. return 0;
  137. }
  138. static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
  139. {
  140. int ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align);
  141. pkt->stream_index = 0;
  142. if (ret <= 0)
  143. return AVERROR(EIO);
  144. return ret;
  145. }
  146. static int oma_read_probe(AVProbeData *p)
  147. {
  148. const uint8_t *buf;
  149. unsigned tag_len = 0;
  150. buf = p->buf;
  151. /* version must be 3 and flags byte zero */
  152. if (ff_id3v2_match(buf, ID3v2_EA3_MAGIC) && buf[3] == 3 && !buf[4])
  153. tag_len = ff_id3v2_tag_len(buf);
  154. // This check cannot overflow as tag_len has at most 28 bits
  155. if (p->buf_size < tag_len + 5)
  156. return 0;
  157. buf += tag_len;
  158. if (!memcmp(buf, "EA3", 3) && !buf[4] && buf[5] == EA3_HEADER_SIZE)
  159. return AVPROBE_SCORE_MAX;
  160. else
  161. return 0;
  162. }
  163. AVInputFormat ff_oma_demuxer = {
  164. .name = "oma",
  165. .long_name = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
  166. .read_probe = oma_read_probe,
  167. .read_header = oma_read_header,
  168. .read_packet = oma_read_packet,
  169. .read_seek = pcm_read_seek,
  170. .flags= AVFMT_GENERIC_INDEX,
  171. .extensions = "oma,aa3",
  172. .codec_tag= (const AVCodecTag* const []){codec_oma_tags, 0},
  173. };