oma.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. ea3_taglen = ((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) | ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f);
  74. EA3_pos = ea3_taglen + 10;
  75. if (buf[5] & 0x10)
  76. EA3_pos += 10;
  77. url_fseek(s->pb, EA3_pos, SEEK_SET);
  78. ret = get_buffer(s->pb, buf, EA3_HEADER_SIZE);
  79. if (ret != EA3_HEADER_SIZE)
  80. return -1;
  81. if (memcmp(buf, (const uint8_t[]){'E', 'A', '3'},3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) {
  82. av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n");
  83. return -1;
  84. }
  85. eid = AV_RB16(&buf[6]);
  86. if (eid != -1 && eid != -128) {
  87. av_log(s, AV_LOG_ERROR, "Encrypted file! Eid: %d\n", eid);
  88. return -1;
  89. }
  90. codec_params = AV_RB24(&buf[33]);
  91. st = av_new_stream(s, 0);
  92. if (!st)
  93. return AVERROR(ENOMEM);
  94. st->start_time = 0;
  95. st->codec->codec_type = CODEC_TYPE_AUDIO;
  96. st->codec->codec_tag = buf[32];
  97. st->codec->codec_id = codec_get_id(codec_oma_tags, st->codec->codec_tag);
  98. switch (buf[32]) {
  99. case OMA_CODECID_ATRAC3:
  100. samplerate = srate_tab[(codec_params >> 13) & 7]*100;
  101. if (samplerate != 44100)
  102. av_log(s, AV_LOG_ERROR, "Unsupported sample rate, send sample file to developers: %d\n", samplerate);
  103. framesize = (codec_params & 0x3FF) * 8;
  104. jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */
  105. st->codec->channels = 2;
  106. st->codec->sample_rate = samplerate;
  107. st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
  108. /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */
  109. st->codec->extradata_size = 14;
  110. edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
  111. if (!edata)
  112. return AVERROR(ENOMEM);
  113. st->codec->extradata = edata;
  114. AV_WL16(&edata[0], 1); // always 1
  115. AV_WL32(&edata[2], samplerate); // samples rate
  116. AV_WL16(&edata[6], jsflag); // coding mode
  117. AV_WL16(&edata[8], jsflag); // coding mode
  118. AV_WL16(&edata[10], 1); // always 1
  119. // AV_WL16(&edata[12], 0); // always 0
  120. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  121. break;
  122. case OMA_CODECID_ATRAC3P:
  123. st->codec->channels = (codec_params >> 10) & 7;
  124. framesize = ((codec_params & 0x3FF) * 8) + 8;
  125. st->codec->sample_rate = srate_tab[(codec_params >> 13) & 7]*100;
  126. st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
  127. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  128. av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
  129. break;
  130. case OMA_CODECID_MP3:
  131. st->need_parsing = AVSTREAM_PARSE_FULL;
  132. framesize = 1024;
  133. break;
  134. default:
  135. av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
  136. return -1;
  137. break;
  138. }
  139. st->codec->block_align = framesize;
  140. url_fseek(s->pb, EA3_pos + EA3_HEADER_SIZE, SEEK_SET);
  141. return 0;
  142. }
  143. static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
  144. {
  145. int ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align);
  146. pkt->stream_index = 0;
  147. if (ret <= 0)
  148. return AVERROR(EIO);
  149. return ret;
  150. }
  151. static int oma_read_probe(AVProbeData *p)
  152. {
  153. if (!memcmp(p->buf, (const uint8_t[]){'e', 'a', '3', 3, 0},5))
  154. return AVPROBE_SCORE_MAX;
  155. else
  156. return 0;
  157. }
  158. AVInputFormat oma_demuxer = {
  159. "oma",
  160. NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
  161. 0,
  162. oma_read_probe,
  163. oma_read_header,
  164. oma_read_packet,
  165. 0,
  166. pcm_read_seek,
  167. .flags= AVFMT_GENERIC_INDEX,
  168. .extensions = "oma,aa3",
  169. .codec_tag= (const AVCodecTag* const []){codec_oma_tags, 0},
  170. };