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.

375 lines
10KB

  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/channel_layout.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #define DSS_HEAD_OFFSET_AUTHOR 0xc
  26. #define DSS_AUTHOR_SIZE 16
  27. #define DSS_HEAD_OFFSET_START_TIME 0x26
  28. #define DSS_HEAD_OFFSET_END_TIME 0x32
  29. #define DSS_TIME_SIZE 12
  30. #define DSS_HEAD_OFFSET_ACODEC 0x2a4
  31. #define DSS_ACODEC_DSS_SP 0x0 /* SP mode */
  32. #define DSS_ACODEC_G723_1 0x2 /* LP mode */
  33. #define DSS_HEAD_OFFSET_COMMENT 0x31e
  34. #define DSS_COMMENT_SIZE 64
  35. #define DSS_BLOCK_SIZE 512
  36. #define DSS_AUDIO_BLOCK_HEADER_SIZE 6
  37. #define DSS_FRAME_SIZE 42
  38. static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
  39. typedef struct DSSDemuxContext {
  40. unsigned int audio_codec;
  41. int counter;
  42. int swap;
  43. int dss_sp_swap_byte;
  44. int8_t dss_sp_buf[DSS_FRAME_SIZE + 1];
  45. int packet_size;
  46. int dss_header_size;
  47. } DSSDemuxContext;
  48. static int dss_probe(const AVProbeData *p)
  49. {
  50. if ( AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's')
  51. && AV_RL32(p->buf) != MKTAG(0x3, '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. av_free(value);
  87. return ret < 0 ? ret : AVERROR_EOF;
  88. }
  89. return av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  90. }
  91. static int dss_read_header(AVFormatContext *s)
  92. {
  93. DSSDemuxContext *ctx = s->priv_data;
  94. AVIOContext *pb = s->pb;
  95. AVStream *st;
  96. int ret, version;
  97. st = avformat_new_stream(s, NULL);
  98. if (!st)
  99. return AVERROR(ENOMEM);
  100. version = avio_r8(pb);
  101. ctx->dss_header_size = version * DSS_BLOCK_SIZE;
  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 = 11025;
  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, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
  133. return AVERROR(EIO);
  134. ctx->counter = 0;
  135. ctx->swap = 0;
  136. return 0;
  137. }
  138. static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
  139. {
  140. DSSDemuxContext *ctx = s->priv_data;
  141. AVIOContext *pb = s->pb;
  142. avio_skip(pb, DSS_AUDIO_BLOCK_HEADER_SIZE);
  143. ctx->counter += DSS_BLOCK_SIZE - DSS_AUDIO_BLOCK_HEADER_SIZE;
  144. }
  145. static void dss_sp_byte_swap(DSSDemuxContext *ctx,
  146. uint8_t *dst, const uint8_t *src)
  147. {
  148. int i;
  149. if (ctx->swap) {
  150. for (i = 3; i < DSS_FRAME_SIZE; i += 2)
  151. dst[i] = src[i];
  152. for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
  153. dst[i] = src[i + 4];
  154. dst[1] = ctx->dss_sp_swap_byte;
  155. } else {
  156. memcpy(dst, src, DSS_FRAME_SIZE);
  157. ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
  158. }
  159. /* make sure byte 40 is always 0 */
  160. dst[DSS_FRAME_SIZE - 2] = 0;
  161. ctx->swap ^= 1;
  162. }
  163. static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
  164. {
  165. DSSDemuxContext *ctx = s->priv_data;
  166. AVStream *st = s->streams[0];
  167. int read_size, ret, offset = 0, buff_offset = 0;
  168. int64_t pos = avio_tell(s->pb);
  169. if (ctx->counter == 0)
  170. dss_skip_audio_header(s, pkt);
  171. if (ctx->swap) {
  172. read_size = DSS_FRAME_SIZE - 2;
  173. buff_offset = 3;
  174. } else
  175. read_size = DSS_FRAME_SIZE;
  176. ctx->counter -= read_size;
  177. ctx->packet_size = DSS_FRAME_SIZE - 1;
  178. ret = av_new_packet(pkt, DSS_FRAME_SIZE);
  179. if (ret < 0)
  180. return ret;
  181. pkt->duration = 264;
  182. pkt->pos = pos;
  183. pkt->stream_index = 0;
  184. s->bit_rate = 8LL * ctx->packet_size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
  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 (ctx->dss_sp_swap_byte < 0) {
  200. return AVERROR(EAGAIN);
  201. }
  202. return pkt->size;
  203. error_eof:
  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. AVStream *st = s->streams[0];
  210. int size, byte, ret, offset;
  211. int64_t pos = avio_tell(s->pb);
  212. if (ctx->counter == 0)
  213. dss_skip_audio_header(s, pkt);
  214. /* We make one byte-step here. Don't forget to add offset. */
  215. byte = avio_r8(s->pb);
  216. if (byte == 0xff)
  217. return AVERROR_INVALIDDATA;
  218. size = frame_size[byte & 3];
  219. ctx->packet_size = size;
  220. ctx->counter -= size;
  221. ret = av_new_packet(pkt, size);
  222. if (ret < 0)
  223. return ret;
  224. pkt->pos = pos;
  225. pkt->data[0] = byte;
  226. offset = 1;
  227. pkt->duration = 240;
  228. s->bit_rate = 8LL * size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
  229. pkt->stream_index = 0;
  230. if (ctx->counter < 0) {
  231. int size2 = ctx->counter + size;
  232. ret = avio_read(s->pb, pkt->data + offset,
  233. size2 - offset);
  234. if (ret < size2 - offset) {
  235. return ret < 0 ? ret : AVERROR_EOF;
  236. }
  237. dss_skip_audio_header(s, pkt);
  238. offset = size2;
  239. }
  240. ret = avio_read(s->pb, pkt->data + offset, size - offset);
  241. if (ret < size - offset) {
  242. return ret < 0 ? ret : AVERROR_EOF;
  243. }
  244. return pkt->size;
  245. }
  246. static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
  247. {
  248. DSSDemuxContext *ctx = s->priv_data;
  249. if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
  250. return dss_sp_read_packet(s, pkt);
  251. else
  252. return dss_723_1_read_packet(s, pkt);
  253. }
  254. static int dss_read_seek(AVFormatContext *s, int stream_index,
  255. int64_t timestamp, int flags)
  256. {
  257. DSSDemuxContext *ctx = s->priv_data;
  258. int64_t ret, seekto;
  259. uint8_t header[DSS_AUDIO_BLOCK_HEADER_SIZE];
  260. int offset;
  261. if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
  262. seekto = timestamp / 264 * 41 / 506 * 512;
  263. else
  264. seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
  265. if (seekto < 0)
  266. seekto = 0;
  267. seekto += ctx->dss_header_size;
  268. ret = avio_seek(s->pb, seekto, SEEK_SET);
  269. if (ret < 0)
  270. return ret;
  271. avio_read(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
  272. ctx->swap = !!(header[0] & 0x80);
  273. offset = 2*header[1] + 2*ctx->swap;
  274. if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
  275. return AVERROR_INVALIDDATA;
  276. if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
  277. ctx->counter = 0;
  278. offset = avio_skip(s->pb, -DSS_AUDIO_BLOCK_HEADER_SIZE);
  279. } else {
  280. ctx->counter = DSS_BLOCK_SIZE - offset;
  281. offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
  282. }
  283. ctx->dss_sp_swap_byte = -1;
  284. return 0;
  285. }
  286. AVInputFormat ff_dss_demuxer = {
  287. .name = "dss",
  288. .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
  289. .priv_data_size = sizeof(DSSDemuxContext),
  290. .read_probe = dss_probe,
  291. .read_header = dss_read_header,
  292. .read_packet = dss_read_packet,
  293. .read_seek = dss_read_seek,
  294. .extensions = "dss"
  295. };