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.

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