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.

372 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->packet_size = DSS_FRAME_SIZE - 1;
  177. ret = av_new_packet(pkt, DSS_FRAME_SIZE);
  178. if (ret < 0)
  179. return ret;
  180. pkt->duration = 264;
  181. pkt->pos = pos;
  182. pkt->stream_index = 0;
  183. s->bit_rate = 8LL * ctx->packet_size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
  184. if (ctx->counter < read_size) {
  185. ret = avio_read(s->pb, ctx->dss_sp_buf + buff_offset,
  186. ctx->counter);
  187. if (ret < ctx->counter)
  188. goto error_eof;
  189. offset = ctx->counter;
  190. dss_skip_audio_header(s, pkt);
  191. }
  192. ctx->counter -= read_size;
  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 (ctx->dss_sp_swap_byte < 0) {
  199. return AVERROR(EAGAIN);
  200. }
  201. return pkt->size;
  202. error_eof:
  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. AVStream *st = s->streams[0];
  209. int size, byte, ret, offset;
  210. int64_t pos = avio_tell(s->pb);
  211. if (ctx->counter == 0)
  212. dss_skip_audio_header(s, pkt);
  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->packet_size = size;
  219. ctx->counter--;
  220. ret = av_new_packet(pkt, size);
  221. if (ret < 0)
  222. return ret;
  223. pkt->pos = pos;
  224. pkt->data[0] = byte;
  225. offset = 1;
  226. pkt->duration = 240;
  227. s->bit_rate = 8LL * size-- * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
  228. pkt->stream_index = 0;
  229. if (ctx->counter < size) {
  230. ret = avio_read(s->pb, pkt->data + offset,
  231. ctx->counter);
  232. if (ret < ctx->counter)
  233. return ret < 0 ? ret : AVERROR_EOF;
  234. offset += ctx->counter;
  235. size -= ctx->counter;
  236. ctx->counter = 0;
  237. dss_skip_audio_header(s, pkt);
  238. }
  239. ctx->counter -= size;
  240. ret = avio_read(s->pb, pkt->data + offset, size);
  241. if (ret < size)
  242. return ret < 0 ? ret : AVERROR_EOF;
  243. return pkt->size;
  244. }
  245. static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
  246. {
  247. DSSDemuxContext *ctx = s->priv_data;
  248. if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
  249. return dss_sp_read_packet(s, pkt);
  250. else
  251. return dss_723_1_read_packet(s, pkt);
  252. }
  253. static int dss_read_seek(AVFormatContext *s, int stream_index,
  254. int64_t timestamp, int flags)
  255. {
  256. DSSDemuxContext *ctx = s->priv_data;
  257. int64_t ret, seekto;
  258. uint8_t header[DSS_AUDIO_BLOCK_HEADER_SIZE];
  259. int offset;
  260. if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
  261. seekto = timestamp / 264 * 41 / 506 * 512;
  262. else
  263. seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
  264. if (seekto < 0)
  265. seekto = 0;
  266. seekto += ctx->dss_header_size;
  267. ret = avio_seek(s->pb, seekto, SEEK_SET);
  268. if (ret < 0)
  269. return ret;
  270. avio_read(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
  271. ctx->swap = !!(header[0] & 0x80);
  272. offset = 2*header[1] + 2*ctx->swap;
  273. if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
  274. return AVERROR_INVALIDDATA;
  275. if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
  276. ctx->counter = 0;
  277. offset = avio_skip(s->pb, -DSS_AUDIO_BLOCK_HEADER_SIZE);
  278. } else {
  279. ctx->counter = DSS_BLOCK_SIZE - offset;
  280. offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
  281. }
  282. ctx->dss_sp_swap_byte = -1;
  283. return 0;
  284. }
  285. AVInputFormat ff_dss_demuxer = {
  286. .name = "dss",
  287. .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
  288. .priv_data_size = sizeof(DSSDemuxContext),
  289. .read_probe = dss_probe,
  290. .read_header = dss_read_header,
  291. .read_packet = dss_read_packet,
  292. .read_seek = dss_read_seek,
  293. .extensions = "dss"
  294. };