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.

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