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.

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