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.

203 lines
6.1KB

  1. /*
  2. * AST muxer
  3. * Copyright (c) 2012 James Almer
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "avio_internal.h"
  23. #include "internal.h"
  24. #include "ast.h"
  25. #include "libavutil/mathematics.h"
  26. #include "libavutil/opt.h"
  27. typedef struct ASTMuxContext {
  28. AVClass *class;
  29. int64_t size;
  30. int64_t samples;
  31. int64_t loopstart;
  32. int64_t loopend;
  33. int fbs;
  34. } ASTMuxContext;
  35. #define CHECK_LOOP(type) \
  36. if (ast->loop ## type) { \
  37. ast->loop ## type = av_rescale_q_rnd(ast->loop ## type, (AVRational){enc->sample_rate, 1}, (AVRational){1000, 1}, AV_ROUND_DOWN); \
  38. if (ast->loop ## type < 0 || ast->loop ## type > UINT_MAX) { \
  39. av_log(s, AV_LOG_ERROR, "Invalid loop" #type " value\n"); \
  40. return AVERROR(EINVAL); \
  41. } \
  42. }
  43. static int ast_write_header(AVFormatContext *s)
  44. {
  45. ASTMuxContext *ast = s->priv_data;
  46. AVIOContext *pb = s->pb;
  47. AVCodecContext *enc;
  48. unsigned int codec_tag;
  49. if (s->nb_streams = 1) {
  50. enc = s->streams[0]->codec;
  51. } else {
  52. av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
  53. return AVERROR(EINVAL);
  54. }
  55. if (enc->codec_id == AV_CODEC_ID_ADPCM_AFC) {
  56. av_log(s, AV_LOG_ERROR, "muxing ADPCM AFC is not implemented\n");
  57. return AVERROR_PATCHWELCOME;
  58. }
  59. codec_tag = ff_codec_get_tag(ff_codec_ast_tags, enc->codec_id);
  60. if (!codec_tag) {
  61. av_log(s, AV_LOG_ERROR, "unsupported codec\n");
  62. return AVERROR(EINVAL);
  63. }
  64. if (ast->loopstart && ast->loopend && ast->loopstart >= ast->loopend) {
  65. av_log(s, AV_LOG_ERROR, "loopend can't be less or equal to loopstart\n");
  66. return AVERROR(EINVAL);
  67. }
  68. /* Convert milliseconds to samples */
  69. CHECK_LOOP(start)
  70. CHECK_LOOP(end)
  71. ffio_wfourcc(pb, "STRM");
  72. ast->size = avio_tell(pb);
  73. avio_wb32(pb, 0); /* File size minus header */
  74. avio_wb16(pb, codec_tag);
  75. avio_wb16(pb, 16); /* Bit depth */
  76. avio_wb16(pb, enc->channels);
  77. avio_wb16(pb, 0xFFFF);
  78. avio_wb32(pb, enc->sample_rate);
  79. ast->samples = avio_tell(pb);
  80. avio_wb32(pb, 0); /* Number of samples */
  81. avio_wb32(pb, 0); /* Loopstart */
  82. avio_wb32(pb, 0); /* Loopend */
  83. avio_wb32(pb, 0); /* Size of first block */
  84. /* Unknown */
  85. avio_wb32(pb, 0);
  86. avio_wl32(pb, 0x7F);
  87. avio_wb64(pb, 0);
  88. avio_wb64(pb, 0);
  89. avio_wb32(pb, 0);
  90. avio_flush(pb);
  91. return 0;
  92. }
  93. static int ast_write_packet(AVFormatContext *s, AVPacket *pkt)
  94. {
  95. AVIOContext *pb = s->pb;
  96. ASTMuxContext *ast = s->priv_data;
  97. AVCodecContext *enc = s->streams[0]->codec;
  98. int size = pkt->size / enc->channels;
  99. if (enc->frame_number == 1)
  100. ast->fbs = size;
  101. ffio_wfourcc(pb, "BLCK");
  102. avio_wb32(pb, size); /* Block size */
  103. /* padding */
  104. avio_wb64(pb, 0);
  105. avio_wb64(pb, 0);
  106. avio_wb64(pb, 0);
  107. avio_write(pb, pkt->data, pkt->size);
  108. return 0;
  109. }
  110. static int ast_write_trailer(AVFormatContext *s)
  111. {
  112. AVIOContext *pb = s->pb;
  113. ASTMuxContext *ast = s->priv_data;
  114. AVCodecContext *enc = s->streams[0]->codec;
  115. int64_t file_size = avio_tell(pb);
  116. int64_t samples = (file_size - 64 - (32 * enc->frame_number)) / enc->block_align; /* PCM_S16BE_PLANAR */
  117. av_log(s, AV_LOG_DEBUG, "total samples: %"PRId64"\n", samples);
  118. if (s->pb->seekable) {
  119. /* File size minus header */
  120. avio_seek(pb, ast->size, SEEK_SET);
  121. avio_wb32(pb, file_size - 64);
  122. /* Number of samples */
  123. avio_seek(pb, ast->samples, SEEK_SET);
  124. avio_wb32(pb, samples);
  125. /* Loopstart if provided */
  126. if (ast->loopstart && ast->loopstart >= samples) {
  127. av_log(s, AV_LOG_WARNING, "Loopstart value is out of range and will be ignored\n");
  128. ast->loopstart = 0;
  129. }
  130. avio_wb32(pb, ast->loopstart);
  131. /* Loopend if provided. Otherwise number of samples again */
  132. if (ast->loopend) {
  133. if (ast->loopend > samples) {
  134. av_log(s, AV_LOG_WARNING, "Loopend value is out of range and will be ignored\n");
  135. ast->loopend = samples;
  136. }
  137. avio_wb32(pb, ast->loopend);
  138. } else {
  139. avio_wb32(pb, samples);
  140. }
  141. /* Size of first block */
  142. avio_wb32(pb, ast->fbs);
  143. avio_flush(pb);
  144. }
  145. return 0;
  146. }
  147. #define OFFSET(obj) offsetof(ASTMuxContext, obj)
  148. static const AVOption options[] = {
  149. { "loopstart", "Loopstart position in milliseconds.", OFFSET(loopstart), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  150. { "loopend", "Loopend position in milliseconds.", OFFSET(loopend), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  151. { NULL },
  152. };
  153. static const AVClass ast_muxer_class = {
  154. .class_name = "AST muxer",
  155. .item_name = av_default_item_name,
  156. .option = options,
  157. .version = LIBAVUTIL_VERSION_INT,
  158. };
  159. AVOutputFormat ff_ast_muxer = {
  160. .name = "ast",
  161. .long_name = NULL_IF_CONFIG_SMALL("AST (Audio Stream)"),
  162. .extensions = "ast",
  163. .priv_data_size = sizeof(ASTMuxContext),
  164. .audio_codec = AV_CODEC_ID_PCM_S16BE_PLANAR,
  165. .video_codec = AV_CODEC_ID_NONE,
  166. .write_header = ast_write_header,
  167. .write_packet = ast_write_packet,
  168. .write_trailer = ast_write_trailer,
  169. .priv_class = &ast_muxer_class,
  170. .codec_tag = (const AVCodecTag* const []){ff_codec_ast_tags, 0},
  171. };