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.

215 lines
6.4KB

  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 > 0) { \
  37. ast->loop ## type = av_rescale_rnd(ast->loop ## type, enc->sample_rate, 1000, 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->loopend > 0 && 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, 0); /* Loop flag */
  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 (s->streams[0]->nb_frames == 0)
  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 * s->streams[0]->nb_frames)) / enc->block_align; /* PCM_S16BE_PLANAR */
  117. av_log(s, AV_LOG_DEBUG, "total samples: %"PRId64"\n", samples);
  118. if (s->pb->seekable) {
  119. /* Number of samples */
  120. avio_seek(pb, ast->samples, SEEK_SET);
  121. avio_wb32(pb, samples);
  122. /* Loopstart if provided */
  123. if (ast->loopstart > 0) {
  124. if (ast->loopstart >= samples) {
  125. av_log(s, AV_LOG_WARNING, "Loopstart value is out of range and will be ignored\n");
  126. ast->loopstart = -1;
  127. avio_skip(pb, 4);
  128. } else
  129. avio_wb32(pb, ast->loopstart);
  130. } else
  131. avio_skip(pb, 4);
  132. /* Loopend if provided. Otherwise number of samples again */
  133. if (ast->loopend && ast->loopstart >= 0) {
  134. if (ast->loopend > samples) {
  135. av_log(s, AV_LOG_WARNING, "Loopend value is out of range and will be ignored\n");
  136. ast->loopend = samples;
  137. }
  138. avio_wb32(pb, ast->loopend);
  139. } else {
  140. avio_wb32(pb, samples);
  141. }
  142. /* Size of first block */
  143. avio_wb32(pb, ast->fbs);
  144. /* File size minus header */
  145. avio_seek(pb, ast->size, SEEK_SET);
  146. avio_wb32(pb, file_size - 64);
  147. /* Loop flag */
  148. if (ast->loopstart >= 0) {
  149. avio_skip(pb, 6);
  150. avio_wb16(pb, 0xFFFF);
  151. }
  152. avio_seek(pb, file_size, SEEK_SET);
  153. avio_flush(pb);
  154. }
  155. return 0;
  156. }
  157. #define OFFSET(obj) offsetof(ASTMuxContext, obj)
  158. static const AVOption options[] = {
  159. { "loopstart", "Loopstart position in milliseconds.", OFFSET(loopstart), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  160. { "loopend", "Loopend position in milliseconds.", OFFSET(loopend), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  161. { NULL },
  162. };
  163. static const AVClass ast_muxer_class = {
  164. .class_name = "AST muxer",
  165. .item_name = av_default_item_name,
  166. .option = options,
  167. .version = LIBAVUTIL_VERSION_INT,
  168. };
  169. AVOutputFormat ff_ast_muxer = {
  170. .name = "ast",
  171. .long_name = NULL_IF_CONFIG_SMALL("AST (Audio Stream)"),
  172. .extensions = "ast",
  173. .priv_data_size = sizeof(ASTMuxContext),
  174. .audio_codec = AV_CODEC_ID_PCM_S16BE_PLANAR,
  175. .video_codec = AV_CODEC_ID_NONE,
  176. .write_header = ast_write_header,
  177. .write_packet = ast_write_packet,
  178. .write_trailer = ast_write_trailer,
  179. .priv_class = &ast_muxer_class,
  180. .codec_tag = (const AVCodecTag* const []){ff_codec_ast_tags, 0},
  181. };