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.

286 lines
8.8KB

  1. /*
  2. * codec2 muxer and demuxers
  3. * Copyright (c) 2017 Tomas Härdin
  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 <memory.h>
  22. #include "libavcodec/codec2utils.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "avio_internal.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "rawdec.h"
  28. #include "rawenc.h"
  29. #include "pcm.h"
  30. #define AVPRIV_CODEC2_HEADER_SIZE 7
  31. #define AVPRIV_CODEC2_MAGIC 0xC0DEC2
  32. //the lowest version we should ever run across is 0.8
  33. //we may run across later versions as the format evolves
  34. #define EXPECTED_CODEC2_MAJOR_VERSION 0
  35. #define EXPECTED_CODEC2_MINOR_VERSION 8
  36. typedef struct {
  37. const AVClass *class;
  38. int mode;
  39. int frames_per_packet;
  40. } Codec2Context;
  41. static int codec2_probe(const AVProbeData *p)
  42. {
  43. //must start wih C0 DE C2
  44. if (AV_RB24(p->buf) != AVPRIV_CODEC2_MAGIC) {
  45. return 0;
  46. }
  47. //no .c2 files prior to 0.8
  48. //be strict about major version while we're at it
  49. if (p->buf[3] != EXPECTED_CODEC2_MAJOR_VERSION ||
  50. p->buf[4] < EXPECTED_CODEC2_MINOR_VERSION) {
  51. return 0;
  52. }
  53. //32 bits of identification -> low score
  54. return AVPROBE_SCORE_EXTENSION + 1;
  55. }
  56. static int codec2_read_header_common(AVFormatContext *s, AVStream *st)
  57. {
  58. int mode = avpriv_codec2_mode_from_extradata(st->codecpar->extradata);
  59. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  60. st->codecpar->codec_id = AV_CODEC_ID_CODEC2;
  61. st->codecpar->sample_rate = 8000;
  62. st->codecpar->channels = 1;
  63. st->codecpar->format = AV_SAMPLE_FMT_S16;
  64. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  65. st->codecpar->bit_rate = avpriv_codec2_mode_bit_rate(s, mode);
  66. st->codecpar->frame_size = avpriv_codec2_mode_frame_size(s, mode);
  67. st->codecpar->block_align = avpriv_codec2_mode_block_align(s, mode);
  68. if (st->codecpar->bit_rate <= 0 ||
  69. st->codecpar->frame_size <= 0 ||
  70. st->codecpar->block_align <= 0) {
  71. return AVERROR_INVALIDDATA;
  72. }
  73. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  74. return 0;
  75. }
  76. static int codec2_read_header(AVFormatContext *s)
  77. {
  78. AVStream *st = avformat_new_stream(s, NULL);
  79. int ret, version;
  80. if (!st) {
  81. return AVERROR(ENOMEM);
  82. }
  83. if (avio_rb24(s->pb) != AVPRIV_CODEC2_MAGIC) {
  84. av_log(s, AV_LOG_ERROR, "not a .c2 file\n");
  85. return AVERROR_INVALIDDATA;
  86. }
  87. ret = ff_alloc_extradata(st->codecpar, AVPRIV_CODEC2_EXTRADATA_SIZE);
  88. if (ret) {
  89. return ret;
  90. }
  91. ret = ffio_read_size(s->pb, st->codecpar->extradata, AVPRIV_CODEC2_EXTRADATA_SIZE);
  92. if (ret < 0) {
  93. return ret;
  94. }
  95. version = avpriv_codec2_version_from_extradata(st->codecpar->extradata);
  96. if ((version >> 8) != EXPECTED_CODEC2_MAJOR_VERSION) {
  97. avpriv_report_missing_feature(s, "Major version %i", version >> 8);
  98. return AVERROR_PATCHWELCOME;
  99. }
  100. s->internal->data_offset = AVPRIV_CODEC2_HEADER_SIZE;
  101. return codec2_read_header_common(s, st);
  102. }
  103. static int codec2_read_packet(AVFormatContext *s, AVPacket *pkt)
  104. {
  105. Codec2Context *c2 = s->priv_data;
  106. AVStream *st = s->streams[0];
  107. int ret, size, n, block_align, frame_size;
  108. block_align = st->codecpar->block_align;
  109. frame_size = st->codecpar->frame_size;
  110. if (block_align <= 0 || frame_size <= 0 || c2->frames_per_packet <= 0) {
  111. return AVERROR(EINVAL);
  112. }
  113. //try to read desired number of frames, compute n from to actual number of bytes read
  114. size = c2->frames_per_packet * block_align;
  115. ret = av_get_packet(s->pb, pkt, size);
  116. if (ret < 0) {
  117. return ret;
  118. }
  119. //only set duration - compute_pkt_fields() and ff_pcm_read_seek() takes care of everything else
  120. //tested by spamming the seek functionality in ffplay
  121. n = ret / block_align;
  122. pkt->duration = n * frame_size;
  123. return ret;
  124. }
  125. static int codec2_write_header(AVFormatContext *s)
  126. {
  127. AVStream *st;
  128. if (s->nb_streams != 1 || s->streams[0]->codecpar->codec_id != AV_CODEC_ID_CODEC2) {
  129. av_log(s, AV_LOG_ERROR, ".c2 files must have exactly one codec2 stream\n");
  130. return AVERROR(EINVAL);
  131. }
  132. st = s->streams[0];
  133. if (st->codecpar->extradata_size != AVPRIV_CODEC2_EXTRADATA_SIZE) {
  134. av_log(s, AV_LOG_ERROR, ".c2 files require exactly %i bytes of extradata (got %i)\n",
  135. AVPRIV_CODEC2_EXTRADATA_SIZE, st->codecpar->extradata_size);
  136. return AVERROR(EINVAL);
  137. }
  138. avio_wb24(s->pb, AVPRIV_CODEC2_MAGIC);
  139. avio_write(s->pb, st->codecpar->extradata, AVPRIV_CODEC2_EXTRADATA_SIZE);
  140. return 0;
  141. }
  142. static int codec2raw_read_header(AVFormatContext *s)
  143. {
  144. Codec2Context *c2 = s->priv_data;
  145. AVStream *st;
  146. int ret;
  147. if (c2->mode < 0) {
  148. //FIXME: using a default value of -1 for mandatory options is an incredibly ugly hack
  149. av_log(s, AV_LOG_ERROR, "-mode must be set in order to make sense of raw codec2 files\n");
  150. return AVERROR(EINVAL);
  151. }
  152. st = avformat_new_stream(s, NULL);
  153. if (!st) {
  154. return AVERROR(ENOMEM);
  155. }
  156. ret = ff_alloc_extradata(st->codecpar, AVPRIV_CODEC2_EXTRADATA_SIZE);
  157. if (ret) {
  158. return ret;
  159. }
  160. s->internal->data_offset = 0;
  161. avpriv_codec2_make_extradata(st->codecpar->extradata, c2->mode);
  162. return codec2_read_header_common(s, st);
  163. }
  164. //transcoding report2074.c2 to wav went from 7.391s to 5.322s with -frames_per_packet 1000 compared to default, same sha1sum
  165. #define FRAMES_PER_PACKET \
  166. { "frames_per_packet", "Number of frames to read at a time. Higher = faster decoding, lower granularity", \
  167. offsetof(Codec2Context, frames_per_packet), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM}
  168. static const AVOption codec2_options[] = {
  169. FRAMES_PER_PACKET,
  170. { NULL },
  171. };
  172. static const AVOption codec2raw_options[] = {
  173. AVPRIV_CODEC2_AVOPTIONS("codec2 mode [mandatory]", Codec2Context, -1, -1, AV_OPT_FLAG_DECODING_PARAM),
  174. FRAMES_PER_PACKET,
  175. { NULL },
  176. };
  177. static const AVClass codec2_mux_class = {
  178. .class_name = "codec2 muxer",
  179. .item_name = av_default_item_name,
  180. .version = LIBAVUTIL_VERSION_INT,
  181. .category = AV_CLASS_CATEGORY_DEMUXER,
  182. };
  183. static const AVClass codec2_demux_class = {
  184. .class_name = "codec2 demuxer",
  185. .item_name = av_default_item_name,
  186. .option = codec2_options,
  187. .version = LIBAVUTIL_VERSION_INT,
  188. .category = AV_CLASS_CATEGORY_DEMUXER,
  189. };
  190. static const AVClass codec2raw_demux_class = {
  191. .class_name = "codec2raw demuxer",
  192. .item_name = av_default_item_name,
  193. .option = codec2raw_options,
  194. .version = LIBAVUTIL_VERSION_INT,
  195. .category = AV_CLASS_CATEGORY_DEMUXER,
  196. };
  197. #if CONFIG_CODEC2_DEMUXER
  198. AVInputFormat ff_codec2_demuxer = {
  199. .name = "codec2",
  200. .long_name = NULL_IF_CONFIG_SMALL("codec2 .c2 demuxer"),
  201. .priv_data_size = sizeof(Codec2Context),
  202. .extensions = "c2",
  203. .read_probe = codec2_probe,
  204. .read_header = codec2_read_header,
  205. .read_packet = codec2_read_packet,
  206. .read_seek = ff_pcm_read_seek,
  207. .flags = AVFMT_GENERIC_INDEX,
  208. .raw_codec_id = AV_CODEC_ID_CODEC2,
  209. .priv_class = &codec2_demux_class,
  210. };
  211. #endif
  212. #if CONFIG_CODEC2_MUXER
  213. AVOutputFormat ff_codec2_muxer = {
  214. .name = "codec2",
  215. .long_name = NULL_IF_CONFIG_SMALL("codec2 .c2 muxer"),
  216. .priv_data_size = sizeof(Codec2Context),
  217. .extensions = "c2",
  218. .audio_codec = AV_CODEC_ID_CODEC2,
  219. .video_codec = AV_CODEC_ID_NONE,
  220. .write_header = codec2_write_header,
  221. .write_packet = ff_raw_write_packet,
  222. .flags = AVFMT_NOTIMESTAMPS,
  223. .priv_class = &codec2_mux_class,
  224. };
  225. #endif
  226. #if CONFIG_CODEC2RAW_DEMUXER
  227. AVInputFormat ff_codec2raw_demuxer = {
  228. .name = "codec2raw",
  229. .long_name = NULL_IF_CONFIG_SMALL("raw codec2 demuxer"),
  230. .priv_data_size = sizeof(Codec2Context),
  231. .read_header = codec2raw_read_header,
  232. .read_packet = codec2_read_packet,
  233. .read_seek = ff_pcm_read_seek,
  234. .flags = AVFMT_GENERIC_INDEX,
  235. .raw_codec_id = AV_CODEC_ID_CODEC2,
  236. .priv_class = &codec2raw_demux_class,
  237. };
  238. #endif