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.

344 lines
9.1KB

  1. /*
  2. * Digital Speech Standard (DSS) demuxer
  3. * Copyright (c) 2014 Oleksij Rempel <linux@rempel-privat.de>
  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 "libavutil/attributes.h"
  22. #include "libavutil/bswap.h"
  23. #include "libavutil/channel_layout.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #define DSS_HEAD_OFFSET_AUTHOR 0xc
  28. #define DSS_AUTHOR_SIZE 16
  29. #define DSS_HEAD_OFFSET_START_TIME 0x26
  30. #define DSS_HEAD_OFFSET_END_TIME 0x32
  31. #define DSS_TIME_SIZE 12
  32. #define DSS_HEAD_OFFSET_ACODEC 0x2a4
  33. #define DSS_ACODEC_DSS_SP 0x0 /* SP mode */
  34. #define DSS_ACODEC_G723_1 0x2 /* LP mode */
  35. #define DSS_HEAD_OFFSET_COMMENT 0x31e
  36. #define DSS_COMMENT_SIZE 64
  37. #define DSS_BLOCK_SIZE 512
  38. #define DSS_HEADER_SIZE (DSS_BLOCK_SIZE * 2)
  39. #define DSS_AUDIO_BLOCK_HEADER_SIZE 6
  40. #define DSS_FRAME_SIZE 42
  41. static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
  42. typedef struct DSSDemuxContext {
  43. unsigned int audio_codec;
  44. int counter;
  45. int swap;
  46. int dss_sp_swap_byte;
  47. int8_t *dss_sp_buf;
  48. } DSSDemuxContext;
  49. static int dss_probe(AVProbeData *p)
  50. {
  51. if (AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's'))
  52. return 0;
  53. return AVPROBE_SCORE_MAX;
  54. }
  55. static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
  56. const char *key)
  57. {
  58. AVIOContext *pb = s->pb;
  59. char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
  60. int y, month, d, h, minute, sec;
  61. int ret;
  62. avio_seek(pb, offset, SEEK_SET);
  63. ret = avio_read(s->pb, string, DSS_TIME_SIZE);
  64. if (ret < DSS_TIME_SIZE)
  65. return ret < 0 ? ret : AVERROR_EOF;
  66. if (sscanf(string, "%2d%2d%2d%2d%2d%2d", &y, &month, &d, &h, &minute, &sec) != 6)
  67. return AVERROR_INVALIDDATA;
  68. /* We deal with a two-digit year here, so set the default date to 2000
  69. * and hope it will never be used in the next century. */
  70. snprintf(datetime, sizeof(datetime), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
  71. y + 2000, month, d, h, minute, sec);
  72. return av_dict_set(&s->metadata, key, datetime, 0);
  73. }
  74. static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset,
  75. unsigned int size, const char *key)
  76. {
  77. AVIOContext *pb = s->pb;
  78. char *value;
  79. int ret;
  80. avio_seek(pb, offset, SEEK_SET);
  81. value = av_mallocz(size + 1);
  82. if (!value)
  83. return AVERROR(ENOMEM);
  84. ret = avio_read(s->pb, value, size);
  85. if (ret < size) {
  86. ret = ret < 0 ? ret : AVERROR_EOF;
  87. goto exit;
  88. }
  89. ret = av_dict_set(&s->metadata, key, value, 0);
  90. exit:
  91. av_free(value);
  92. return ret;
  93. }
  94. static int dss_read_header(AVFormatContext *s)
  95. {
  96. DSSDemuxContext *ctx = s->priv_data;
  97. AVIOContext *pb = s->pb;
  98. AVStream *st;
  99. int ret;
  100. st = avformat_new_stream(s, NULL);
  101. if (!st)
  102. return AVERROR(ENOMEM);
  103. ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_AUTHOR,
  104. DSS_AUTHOR_SIZE, "author");
  105. if (ret)
  106. return ret;
  107. ret = dss_read_metadata_date(s, DSS_HEAD_OFFSET_END_TIME, "date");
  108. if (ret)
  109. return ret;
  110. ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_COMMENT,
  111. DSS_COMMENT_SIZE, "comment");
  112. if (ret)
  113. return ret;
  114. avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
  115. ctx->audio_codec = avio_r8(pb);
  116. if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
  117. st->codec->codec_id = AV_CODEC_ID_DSS_SP;
  118. st->codec->sample_rate = 12000;
  119. } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
  120. st->codec->codec_id = AV_CODEC_ID_G723_1;
  121. st->codec->sample_rate = 8000;
  122. } else {
  123. avpriv_request_sample(s, "Support for codec %x in DSS",
  124. ctx->audio_codec);
  125. return AVERROR_PATCHWELCOME;
  126. }
  127. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  128. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  129. st->codec->channels = 1;
  130. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  131. st->start_time = 0;
  132. /* Jump over header */
  133. if (avio_seek(pb, DSS_HEADER_SIZE, SEEK_SET) != DSS_HEADER_SIZE)
  134. return AVERROR(EIO);
  135. ctx->counter = 0;
  136. ctx->swap = 0;
  137. ctx->dss_sp_buf = av_malloc(DSS_FRAME_SIZE + 1);
  138. if (!ctx->dss_sp_buf)
  139. return AVERROR(ENOMEM);
  140. return 0;
  141. }
  142. static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
  143. {
  144. DSSDemuxContext *ctx = s->priv_data;
  145. AVIOContext *pb = s->pb;
  146. avio_skip(pb, DSS_AUDIO_BLOCK_HEADER_SIZE);
  147. ctx->counter += DSS_BLOCK_SIZE - DSS_AUDIO_BLOCK_HEADER_SIZE;
  148. }
  149. static void dss_sp_byte_swap(DSSDemuxContext *ctx,
  150. uint8_t *dst, const uint8_t *src)
  151. {
  152. int i;
  153. if (ctx->swap) {
  154. for (i = 3; i < DSS_FRAME_SIZE; i += 2)
  155. dst[i] = src[i];
  156. for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
  157. dst[i] = src[i + 4];
  158. dst[1] = ctx->dss_sp_swap_byte;
  159. } else {
  160. memcpy(dst, src, DSS_FRAME_SIZE);
  161. ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
  162. }
  163. /* make sure byte 40 is always 0 */
  164. dst[DSS_FRAME_SIZE - 2] = 0;
  165. ctx->swap ^= 1;
  166. }
  167. static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
  168. {
  169. DSSDemuxContext *ctx = s->priv_data;
  170. int read_size, ret, offset = 0, buff_offset = 0;
  171. if (ctx->counter == 0)
  172. dss_skip_audio_header(s, pkt);
  173. pkt->pos = avio_tell(s->pb);
  174. if (ctx->swap) {
  175. read_size = DSS_FRAME_SIZE - 2;
  176. buff_offset = 3;
  177. } else
  178. read_size = DSS_FRAME_SIZE;
  179. ctx->counter -= read_size;
  180. ret = av_new_packet(pkt, DSS_FRAME_SIZE);
  181. if (ret < 0)
  182. return ret;
  183. pkt->duration = 0;
  184. pkt->stream_index = 0;
  185. if (ctx->counter < 0) {
  186. int size2 = ctx->counter + read_size;
  187. ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
  188. size2 - offset);
  189. if (ret < size2 - offset)
  190. goto error_eof;
  191. dss_skip_audio_header(s, pkt);
  192. offset = size2;
  193. }
  194. ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
  195. read_size - offset);
  196. if (ret < read_size - offset)
  197. goto error_eof;
  198. dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
  199. if (pkt->data[0] == 0xff)
  200. return AVERROR_INVALIDDATA;
  201. return pkt->size;
  202. error_eof:
  203. av_free_packet(pkt);
  204. return ret < 0 ? ret : AVERROR_EOF;
  205. }
  206. static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
  207. {
  208. DSSDemuxContext *ctx = s->priv_data;
  209. int size, byte, ret, offset;
  210. if (ctx->counter == 0)
  211. dss_skip_audio_header(s, pkt);
  212. pkt->pos = avio_tell(s->pb);
  213. /* We make one byte-step here. Don't forget to add offset. */
  214. byte = avio_r8(s->pb);
  215. if (byte == 0xff)
  216. return AVERROR_INVALIDDATA;
  217. size = frame_size[byte & 3];
  218. ctx->counter -= size;
  219. ret = av_new_packet(pkt, size);
  220. if (ret < 0)
  221. return ret;
  222. pkt->data[0] = byte;
  223. offset = 1;
  224. pkt->duration = 240;
  225. pkt->stream_index = 0;
  226. if (ctx->counter < 0) {
  227. int size2 = ctx->counter + size;
  228. ret = avio_read(s->pb, pkt->data + offset,
  229. size2 - offset);
  230. if (ret < size2 - offset) {
  231. av_free_packet(pkt);
  232. return ret < 0 ? ret : AVERROR_EOF;
  233. }
  234. dss_skip_audio_header(s, pkt);
  235. offset = size2;
  236. }
  237. ret = avio_read(s->pb, pkt->data + offset, size - offset);
  238. if (ret < size - offset) {
  239. av_free_packet(pkt);
  240. return ret < 0 ? ret : AVERROR_EOF;
  241. }
  242. return pkt->size;
  243. }
  244. static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
  245. {
  246. DSSDemuxContext *ctx = s->priv_data;
  247. if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
  248. return dss_sp_read_packet(s, pkt);
  249. else
  250. return dss_723_1_read_packet(s, pkt);
  251. }
  252. static int dss_read_close(AVFormatContext *s)
  253. {
  254. DSSDemuxContext *ctx = s->priv_data;
  255. av_freep(&ctx->dss_sp_buf);
  256. return 0;
  257. }
  258. AVInputFormat ff_dss_demuxer = {
  259. .name = "dss",
  260. .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
  261. .priv_data_size = sizeof(DSSDemuxContext),
  262. .read_probe = dss_probe,
  263. .read_header = dss_read_header,
  264. .read_packet = dss_read_packet,
  265. .read_close = dss_read_close,
  266. .extensions = "dss"
  267. };