omaenc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Sony OpenMG (OMA) muxer
  3. *
  4. * Copyright (c) 2011 Michael Karcher
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avformat.h"
  23. #include "avio_internal.h"
  24. #include "id3v2.h"
  25. #include "internal.h"
  26. #include "oma.h"
  27. #include "rawenc.h"
  28. static av_cold int oma_write_header(AVFormatContext *s)
  29. {
  30. int i;
  31. AVCodecContext *format;
  32. int srate_index;
  33. int isjointstereo;
  34. format = s->streams[0]->codec;
  35. /* check for support of the format first */
  36. for (srate_index = 0; ; srate_index++) {
  37. if (ff_oma_srate_tab[srate_index] == 0) {
  38. av_log(s, AV_LOG_ERROR, "Sample rate %d not supported in OpenMG audio\n",
  39. format->sample_rate);
  40. return AVERROR(EINVAL);
  41. }
  42. if (ff_oma_srate_tab[srate_index] * 100 == format->sample_rate)
  43. break;
  44. }
  45. /* Metadata; OpenMG does not support ID3v2.4 */
  46. ff_id3v2_write_simple(s, 3, ID3v2_EA3_MAGIC);
  47. ffio_wfourcc(s->pb, "EA3\0");
  48. avio_w8(s->pb, EA3_HEADER_SIZE >> 7);
  49. avio_w8(s->pb, EA3_HEADER_SIZE & 0x7F);
  50. avio_wl16(s->pb, 0xFFFF); /* not encrypted */
  51. for (i = 0; i < 6; i++)
  52. avio_wl32(s->pb, 0); /* Padding + DRM id */
  53. switch(format->codec_tag) {
  54. case OMA_CODECID_ATRAC3:
  55. if (format->channels != 2) {
  56. av_log(s, AV_LOG_ERROR, "ATRAC3 in OMA is only supported with 2 channels\n");
  57. return AVERROR(EINVAL);
  58. }
  59. if (format->extradata_size == 14) /* WAV format extradata */
  60. isjointstereo = format->extradata[6] != 0;
  61. else if(format->extradata_size == 10) /* RM format extradata */
  62. isjointstereo = format->extradata[8] == 0x12;
  63. else {
  64. av_log(s, AV_LOG_ERROR, "ATRAC3: Unsupported extradata size\n");
  65. return AVERROR(EINVAL);
  66. }
  67. avio_wb32(s->pb, (OMA_CODECID_ATRAC3 << 24) |
  68. (isjointstereo << 17) |
  69. (srate_index << 13) |
  70. (format->block_align/8));
  71. break;
  72. case OMA_CODECID_ATRAC3P:
  73. avio_wb32(s->pb, (OMA_CODECID_ATRAC3P << 24) |
  74. (srate_index << 13) |
  75. (format->channels << 10) |
  76. (format->block_align/8 - 1));
  77. break;
  78. default:
  79. av_log(s, AV_LOG_ERROR, "OMA: unsupported codec tag %d for write\n",
  80. format->codec_tag);
  81. }
  82. for (i = 0; i < (EA3_HEADER_SIZE - 36)/4; i++)
  83. avio_wl32(s->pb, 0); /* Padding */
  84. return 0;
  85. }
  86. AVOutputFormat ff_oma_muxer = {
  87. .name = "oma",
  88. .long_name = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
  89. .mime_type = "audio/x-oma",
  90. .extensions = "oma",
  91. .audio_codec = AV_CODEC_ID_ATRAC3,
  92. .write_header = oma_write_header,
  93. .write_packet = ff_raw_write_packet,
  94. .codec_tag = (const AVCodecTag* const []){ff_oma_codec_tags, 0},
  95. };