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.

191 lines
6.0KB

  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 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. };
  52. static const AVCodecTag codec_oma_tags[] = {
  53. { CODEC_ID_ATRAC3, OMA_CODECID_ATRAC3 },
  54. { CODEC_ID_ATRAC3P, OMA_CODECID_ATRAC3P },
  55. };
  56. static int oma_read_header(AVFormatContext *s,
  57. AVFormatParameters *ap)
  58. {
  59. static const uint16_t srate_tab[6] = {320,441,480,882,960,0};
  60. int ret, ea3_taglen, EA3_pos, framesize, jsflag, samplerate, channel_id;
  61. uint32_t codec_params;
  62. int16_t eid;
  63. uint8_t buf[EA3_HEADER_SIZE];
  64. uint8_t *edata;
  65. AVStream *st;
  66. ret = get_buffer(s->pb, buf, 10);
  67. if (ret != 10)
  68. return -1;
  69. ea3_taglen = ((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) | ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f);
  70. EA3_pos = ea3_taglen + 10;
  71. if (buf[5] & 0x10)
  72. EA3_pos += 10;
  73. url_fseek(s->pb, EA3_pos, SEEK_SET);
  74. ret = get_buffer(s->pb, buf, EA3_HEADER_SIZE);
  75. if (ret != EA3_HEADER_SIZE)
  76. return -1;
  77. if (memcmp(buf, (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. channel_id = (codec_params >> 10) & 7;
  88. samplerate = srate_tab[(codec_params >> 13) & 7]*100;
  89. st = av_new_stream(s, 0);
  90. if (!st)
  91. return AVERROR(ENOMEM);
  92. switch (buf[32]) {
  93. case OMA_CODECID_ATRAC3:
  94. if (samplerate != 44100)
  95. av_log(s, AV_LOG_ERROR, "Unsupported sample rate, send sample file to developers: %d\n", samplerate);
  96. framesize = (codec_params & 0x3FF) * 8;
  97. jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */
  98. channel_id = 2;
  99. /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */
  100. st->codec->extradata_size = 14;
  101. edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
  102. if (!edata)
  103. return AVERROR(ENOMEM);
  104. st->codec->extradata = edata;
  105. AV_WL16(&edata[0], 1); // always 1
  106. AV_WL32(&edata[2], samplerate); // samples rate
  107. AV_WL16(&edata[6], jsflag); // coding mode
  108. AV_WL16(&edata[8], jsflag); // coding mode
  109. AV_WL16(&edata[10], 1); // always 1
  110. // AV_WL16(&edata[12], 0); // always 0
  111. break;
  112. case OMA_CODECID_ATRAC3P:
  113. framesize = ((codec_params & 0x3FF) * 8) + 8;
  114. av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
  115. break;
  116. default:
  117. av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
  118. return -1;
  119. break;
  120. }
  121. st->codec->codec_type = CODEC_TYPE_AUDIO;
  122. st->codec->codec_tag = buf[32];
  123. st->codec->codec_id = codec_get_id(codec_oma_tags, st->codec->codec_tag);
  124. st->codec->channels = channel_id;
  125. st->codec->sample_rate = samplerate;
  126. st->codec->bit_rate = samplerate * framesize * 8 / 1024;
  127. st->codec->block_align = framesize;
  128. st->start_time = 0;
  129. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  130. url_fseek(s->pb, EA3_pos + EA3_HEADER_SIZE, SEEK_SET);
  131. return 0;
  132. }
  133. static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
  134. {
  135. int ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align);
  136. pkt->stream_index = 0;
  137. if (ret <= 0)
  138. return AVERROR(EIO);
  139. return ret;
  140. }
  141. static int oma_read_probe(AVProbeData *p)
  142. {
  143. if (!memcmp(p->buf, (uint8_t[]){'e', 'a', '3', 3, 0},5))
  144. return AVPROBE_SCORE_MAX;
  145. else
  146. return 0;
  147. }
  148. AVInputFormat oma_demuxer = {
  149. "oma",
  150. NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
  151. 0,
  152. oma_read_probe,
  153. oma_read_header,
  154. oma_read_packet,
  155. 0,
  156. pcm_read_seek,
  157. .flags= AVFMT_GENERIC_INDEX,
  158. .extensions = "oma,aa3",
  159. .codec_tag= (const AVCodecTag*[]){codec_oma_tags, 0},
  160. };