act.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * ACT file format demuxer
  3. * Copyright (c) 2007-2008 Vladimir Voroshilov
  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 "avformat.h"
  22. #include "riff.h"
  23. #include "internal.h"
  24. #include "libavcodec/get_bits.h"
  25. #define CHUNK_SIZE 512
  26. #define RIFF_TAG MKTAG('R','I','F','F')
  27. #define WAVE_TAG MKTAG('W','A','V','E')
  28. typedef struct{
  29. int bytes_left_in_chunk;
  30. uint8_t audio_buffer[22];///< temporary buffer for ACT frame
  31. char second_packet; ///< 1 - if temporary buffer contains valid (second) G.729 packet
  32. } ACTContext;
  33. static int probe(AVProbeData *p)
  34. {
  35. int i;
  36. if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
  37. (AV_RL32(&p->buf[8]) != WAVE_TAG) ||
  38. (AV_RL32(&p->buf[16]) != 16))
  39. return 0;
  40. //We cant be sure that this is ACT and not regular WAV
  41. if (p->buf_size<512)
  42. return 0;
  43. for(i=44; i<256; i++)
  44. if(p->buf[i])
  45. return 0;
  46. if(p->buf[256]!=0x84)
  47. return 0;
  48. for(i=264; i<512; i++)
  49. if(p->buf[i])
  50. return 0;
  51. return AVPROBE_SCORE_MAX;
  52. }
  53. static int read_header(AVFormatContext *s,
  54. AVFormatParameters *ap)
  55. {
  56. ACTContext* ctx = s->priv_data;
  57. AVIOContext *pb = s->pb;
  58. int size;
  59. AVStream* st;
  60. int min,sec,msec;
  61. st = avformat_new_stream(s, NULL);
  62. if (!st)
  63. return AVERROR(ENOMEM);
  64. avio_skip(pb, 16);
  65. size=avio_rl32(pb);
  66. ff_get_wav_header(pb, st->codec, size);
  67. /*
  68. 8000Hz (Fine-rec) file format has 10 bytes long
  69. packets with 10ms of sound data in them
  70. */
  71. if (st->codec->sample_rate != 8000) {
  72. av_log(s, AV_LOG_ERROR, "Sample rate %d is not supported.\n", st->codec->sample_rate);
  73. return AVERROR_INVALIDDATA;
  74. }
  75. st->codec->frame_size=80;
  76. st->codec->channels=1;
  77. avpriv_set_pts_info(st, 64, 1, 100);
  78. st->codec->codec_id=CODEC_ID_G729;
  79. avio_seek(pb, 257, SEEK_SET);
  80. msec=avio_rl16(pb);
  81. sec=avio_r8(pb);
  82. min=avio_rl32(pb);
  83. st->duration = av_rescale(1000*(min*60+sec)+msec, st->codec->sample_rate, 1000 * st->codec->frame_size);
  84. ctx->bytes_left_in_chunk=CHUNK_SIZE;
  85. avio_seek(pb, 512, SEEK_SET);
  86. return 0;
  87. }
  88. static int read_packet(AVFormatContext *s,
  89. AVPacket *pkt)
  90. {
  91. ACTContext *ctx = s->priv_data;
  92. AVIOContext *pb = s->pb;
  93. int ret;
  94. int frame_size=s->streams[0]->codec->sample_rate==8000?10:22;
  95. if(s->streams[0]->codec->sample_rate==8000)
  96. ret=av_new_packet(pkt, 10);
  97. else
  98. ret=av_new_packet(pkt, 11);
  99. if(ret)
  100. return ret;
  101. if(s->streams[0]->codec->sample_rate==4400 && !ctx->second_packet)
  102. {
  103. ret = avio_read(pb, ctx->audio_buffer, frame_size);
  104. if(ret<0)
  105. return ret;
  106. if(ret!=frame_size)
  107. return AVERROR(EIO);
  108. pkt->data[0]=ctx->audio_buffer[11];
  109. pkt->data[1]=ctx->audio_buffer[0];
  110. pkt->data[2]=ctx->audio_buffer[12];
  111. pkt->data[3]=ctx->audio_buffer[1];
  112. pkt->data[4]=ctx->audio_buffer[13];
  113. pkt->data[5]=ctx->audio_buffer[2];
  114. pkt->data[6]=ctx->audio_buffer[14];
  115. pkt->data[7]=ctx->audio_buffer[3];
  116. pkt->data[8]=ctx->audio_buffer[15];
  117. pkt->data[9]=ctx->audio_buffer[4];
  118. pkt->data[10]=ctx->audio_buffer[16];
  119. ctx->second_packet=1;
  120. }
  121. else if(s->streams[0]->codec->sample_rate==4400 && ctx->second_packet)
  122. {
  123. pkt->data[0]=ctx->audio_buffer[5];
  124. pkt->data[1]=ctx->audio_buffer[17];
  125. pkt->data[2]=ctx->audio_buffer[6];
  126. pkt->data[3]=ctx->audio_buffer[18];
  127. pkt->data[4]=ctx->audio_buffer[7];
  128. pkt->data[5]=ctx->audio_buffer[19];
  129. pkt->data[6]=ctx->audio_buffer[8];
  130. pkt->data[7]=ctx->audio_buffer[20];
  131. pkt->data[8]=ctx->audio_buffer[9];
  132. pkt->data[9]=ctx->audio_buffer[21];
  133. pkt->data[10]=ctx->audio_buffer[10];
  134. ctx->second_packet=0;
  135. }
  136. else // 8000 Hz
  137. {
  138. ret = avio_read(pb, ctx->audio_buffer, frame_size);
  139. if(ret<0)
  140. return ret;
  141. if(ret!=frame_size)
  142. return AVERROR(EIO);
  143. pkt->data[0]=ctx->audio_buffer[5];
  144. pkt->data[1]=ctx->audio_buffer[0];
  145. pkt->data[2]=ctx->audio_buffer[6];
  146. pkt->data[3]=ctx->audio_buffer[1];
  147. pkt->data[4]=ctx->audio_buffer[7];
  148. pkt->data[5]=ctx->audio_buffer[2];
  149. pkt->data[6]=ctx->audio_buffer[8];
  150. pkt->data[7]=ctx->audio_buffer[3];
  151. pkt->data[8]=ctx->audio_buffer[9];
  152. pkt->data[9]=ctx->audio_buffer[4];
  153. }
  154. ctx->bytes_left_in_chunk -= frame_size;
  155. if(ctx->bytes_left_in_chunk < frame_size)
  156. {
  157. avio_skip(pb, ctx->bytes_left_in_chunk);
  158. ctx->bytes_left_in_chunk=CHUNK_SIZE;
  159. }
  160. pkt->duration=1;
  161. return ret;
  162. }
  163. AVInputFormat ff_act_demuxer = {
  164. .name = "act",
  165. .long_name = "ACT Voice file format",
  166. .priv_data_size = sizeof(ACTContext),
  167. .read_probe = probe,
  168. .read_header = read_header,
  169. .read_packet = read_packet,
  170. };