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.

399 lines
11KB

  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_AUDIO_BLOCK_HEADER_SIZE 6
  39. #define DSS_FRAME_SIZE 42
  40. static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
  41. typedef struct DSSDemuxContext {
  42. unsigned int audio_codec;
  43. int counter;
  44. int swap;
  45. int dss_sp_swap_byte;
  46. int8_t *dss_sp_buf;
  47. int packet_size;
  48. int dss_header_size;
  49. } DSSDemuxContext;
  50. static int dss_probe(AVProbeData *p)
  51. {
  52. if ( AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's')
  53. && AV_RL32(p->buf) != MKTAG(0x3, 'd', 's', 's'))
  54. return 0;
  55. return AVPROBE_SCORE_MAX;
  56. }
  57. static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
  58. const char *key)
  59. {
  60. AVIOContext *pb = s->pb;
  61. char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
  62. int y, month, d, h, minute, sec;
  63. int ret;
  64. avio_seek(pb, offset, SEEK_SET);
  65. ret = avio_read(s->pb, string, DSS_TIME_SIZE);
  66. if (ret < DSS_TIME_SIZE)
  67. return ret < 0 ? ret : AVERROR_EOF;
  68. if (sscanf(string, "%2d%2d%2d%2d%2d%2d", &y, &month, &d, &h, &minute, &sec) != 6)
  69. return AVERROR_INVALIDDATA;
  70. /* We deal with a two-digit year here, so set the default date to 2000
  71. * and hope it will never be used in the next century. */
  72. snprintf(datetime, sizeof(datetime), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
  73. y + 2000, month, d, h, minute, sec);
  74. return av_dict_set(&s->metadata, key, datetime, 0);
  75. }
  76. static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset,
  77. unsigned int size, const char *key)
  78. {
  79. AVIOContext *pb = s->pb;
  80. char *value;
  81. int ret;
  82. avio_seek(pb, offset, SEEK_SET);
  83. value = av_mallocz(size + 1);
  84. if (!value)
  85. return AVERROR(ENOMEM);
  86. ret = avio_read(s->pb, value, size);
  87. if (ret < size) {
  88. ret = ret < 0 ? ret : AVERROR_EOF;
  89. goto exit;
  90. }
  91. ret = av_dict_set(&s->metadata, key, value, 0);
  92. exit:
  93. av_free(value);
  94. return ret;
  95. }
  96. static int dss_read_header(AVFormatContext *s)
  97. {
  98. DSSDemuxContext *ctx = s->priv_data;
  99. AVIOContext *pb = s->pb;
  100. AVStream *st;
  101. int ret, version;
  102. st = avformat_new_stream(s, NULL);
  103. if (!st)
  104. return AVERROR(ENOMEM);
  105. version = avio_r8(pb);
  106. ctx->dss_header_size = version * DSS_BLOCK_SIZE;
  107. ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_AUTHOR,
  108. DSS_AUTHOR_SIZE, "author");
  109. if (ret)
  110. return ret;
  111. ret = dss_read_metadata_date(s, DSS_HEAD_OFFSET_END_TIME, "date");
  112. if (ret)
  113. return ret;
  114. ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_COMMENT,
  115. DSS_COMMENT_SIZE, "comment");
  116. if (ret)
  117. return ret;
  118. avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
  119. ctx->audio_codec = avio_r8(pb);
  120. if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
  121. st->codecpar->codec_id = AV_CODEC_ID_DSS_SP;
  122. st->codecpar->sample_rate = 11025;
  123. } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
  124. st->codecpar->codec_id = AV_CODEC_ID_G723_1;
  125. st->codecpar->sample_rate = 8000;
  126. } else {
  127. avpriv_request_sample(s, "Support for codec %x in DSS",
  128. ctx->audio_codec);
  129. return AVERROR_PATCHWELCOME;
  130. }
  131. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  132. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  133. st->codecpar->channels = 1;
  134. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  135. st->start_time = 0;
  136. /* Jump over header */
  137. if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
  138. return AVERROR(EIO);
  139. ctx->counter = 0;
  140. ctx->swap = 0;
  141. ctx->dss_sp_buf = av_malloc(DSS_FRAME_SIZE + 1);
  142. if (!ctx->dss_sp_buf)
  143. return AVERROR(ENOMEM);
  144. return 0;
  145. }
  146. static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
  147. {
  148. DSSDemuxContext *ctx = s->priv_data;
  149. AVIOContext *pb = s->pb;
  150. avio_skip(pb, DSS_AUDIO_BLOCK_HEADER_SIZE);
  151. ctx->counter += DSS_BLOCK_SIZE - DSS_AUDIO_BLOCK_HEADER_SIZE;
  152. }
  153. static void dss_sp_byte_swap(DSSDemuxContext *ctx,
  154. uint8_t *dst, const uint8_t *src)
  155. {
  156. int i;
  157. if (ctx->swap) {
  158. for (i = 3; i < DSS_FRAME_SIZE; i += 2)
  159. dst[i] = src[i];
  160. for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
  161. dst[i] = src[i + 4];
  162. dst[1] = ctx->dss_sp_swap_byte;
  163. } else {
  164. memcpy(dst, src, DSS_FRAME_SIZE);
  165. ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
  166. }
  167. /* make sure byte 40 is always 0 */
  168. dst[DSS_FRAME_SIZE - 2] = 0;
  169. ctx->swap ^= 1;
  170. }
  171. static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
  172. {
  173. DSSDemuxContext *ctx = s->priv_data;
  174. AVStream *st = s->streams[0];
  175. int read_size, ret, offset = 0, buff_offset = 0;
  176. int64_t pos = avio_tell(s->pb);
  177. if (ctx->counter == 0)
  178. dss_skip_audio_header(s, pkt);
  179. if (ctx->swap) {
  180. read_size = DSS_FRAME_SIZE - 2;
  181. buff_offset = 3;
  182. } else
  183. read_size = DSS_FRAME_SIZE;
  184. ctx->counter -= read_size;
  185. ctx->packet_size = DSS_FRAME_SIZE - 1;
  186. ret = av_new_packet(pkt, DSS_FRAME_SIZE);
  187. if (ret < 0)
  188. return ret;
  189. pkt->duration = 264;
  190. pkt->pos = pos;
  191. pkt->stream_index = 0;
  192. s->bit_rate = 8LL * ctx->packet_size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
  193. if (ctx->counter < 0) {
  194. int size2 = ctx->counter + read_size;
  195. ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
  196. size2 - offset);
  197. if (ret < size2 - offset)
  198. goto error_eof;
  199. dss_skip_audio_header(s, pkt);
  200. offset = size2;
  201. }
  202. ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
  203. read_size - offset);
  204. if (ret < read_size - offset)
  205. goto error_eof;
  206. dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
  207. if (ctx->dss_sp_swap_byte < 0) {
  208. ret = AVERROR(EAGAIN);
  209. goto error_eof;
  210. }
  211. return pkt->size;
  212. error_eof:
  213. av_packet_unref(pkt);
  214. return ret < 0 ? ret : AVERROR_EOF;
  215. }
  216. static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
  217. {
  218. DSSDemuxContext *ctx = s->priv_data;
  219. AVStream *st = s->streams[0];
  220. int size, byte, ret, offset;
  221. int64_t pos = avio_tell(s->pb);
  222. if (ctx->counter == 0)
  223. dss_skip_audio_header(s, pkt);
  224. /* We make one byte-step here. Don't forget to add offset. */
  225. byte = avio_r8(s->pb);
  226. if (byte == 0xff)
  227. return AVERROR_INVALIDDATA;
  228. size = frame_size[byte & 3];
  229. ctx->packet_size = size;
  230. ctx->counter -= size;
  231. ret = av_new_packet(pkt, size);
  232. if (ret < 0)
  233. return ret;
  234. pkt->pos = pos;
  235. pkt->data[0] = byte;
  236. offset = 1;
  237. pkt->duration = 240;
  238. s->bit_rate = 8LL * size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
  239. pkt->stream_index = 0;
  240. if (ctx->counter < 0) {
  241. int size2 = ctx->counter + size;
  242. ret = avio_read(s->pb, pkt->data + offset,
  243. size2 - offset);
  244. if (ret < size2 - offset) {
  245. av_packet_unref(pkt);
  246. return ret < 0 ? ret : AVERROR_EOF;
  247. }
  248. dss_skip_audio_header(s, pkt);
  249. offset = size2;
  250. }
  251. ret = avio_read(s->pb, pkt->data + offset, size - offset);
  252. if (ret < size - offset) {
  253. av_packet_unref(pkt);
  254. return ret < 0 ? ret : AVERROR_EOF;
  255. }
  256. return pkt->size;
  257. }
  258. static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
  259. {
  260. DSSDemuxContext *ctx = s->priv_data;
  261. if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
  262. return dss_sp_read_packet(s, pkt);
  263. else
  264. return dss_723_1_read_packet(s, pkt);
  265. }
  266. static int dss_read_close(AVFormatContext *s)
  267. {
  268. DSSDemuxContext *ctx = s->priv_data;
  269. av_freep(&ctx->dss_sp_buf);
  270. return 0;
  271. }
  272. static int dss_read_seek(AVFormatContext *s, int stream_index,
  273. int64_t timestamp, int flags)
  274. {
  275. DSSDemuxContext *ctx = s->priv_data;
  276. int64_t ret, seekto;
  277. uint8_t header[DSS_AUDIO_BLOCK_HEADER_SIZE];
  278. int offset;
  279. if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
  280. seekto = timestamp / 264 * 41 / 506 * 512;
  281. else
  282. seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
  283. if (seekto < 0)
  284. seekto = 0;
  285. seekto += ctx->dss_header_size;
  286. ret = avio_seek(s->pb, seekto, SEEK_SET);
  287. if (ret < 0)
  288. return ret;
  289. avio_read(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
  290. ctx->swap = !!(header[0] & 0x80);
  291. offset = 2*header[1] + 2*ctx->swap;
  292. if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
  293. return AVERROR_INVALIDDATA;
  294. if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
  295. ctx->counter = 0;
  296. offset = avio_skip(s->pb, -DSS_AUDIO_BLOCK_HEADER_SIZE);
  297. } else {
  298. ctx->counter = DSS_BLOCK_SIZE - offset;
  299. offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
  300. }
  301. ctx->dss_sp_swap_byte = -1;
  302. return 0;
  303. }
  304. AVInputFormat ff_dss_demuxer = {
  305. .name = "dss",
  306. .long_name = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
  307. .priv_data_size = sizeof(DSSDemuxContext),
  308. .read_probe = dss_probe,
  309. .read_header = dss_read_header,
  310. .read_packet = dss_read_packet,
  311. .read_close = dss_read_close,
  312. .read_seek = dss_read_seek,
  313. .extensions = "dss"
  314. };