act.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. {
  55. ACTContext* ctx = s->priv_data;
  56. AVIOContext *pb = s->pb;
  57. int size;
  58. AVStream* st;
  59. int min,sec,msec;
  60. st = avformat_new_stream(s, NULL);
  61. if (!st)
  62. return AVERROR(ENOMEM);
  63. avio_skip(pb, 16);
  64. size=avio_rl32(pb);
  65. ff_get_wav_header(pb, st->codec, size);
  66. /*
  67. 8000Hz (Fine-rec) file format has 10 bytes long
  68. packets with 10ms of sound data in them
  69. */
  70. if (st->codec->sample_rate != 8000) {
  71. av_log(s, AV_LOG_ERROR, "Sample rate %d is not supported.\n", st->codec->sample_rate);
  72. return AVERROR_INVALIDDATA;
  73. }
  74. st->codec->frame_size=80;
  75. st->codec->channels=1;
  76. avpriv_set_pts_info(st, 64, 1, 100);
  77. st->codec->codec_id=AV_CODEC_ID_G729;
  78. avio_seek(pb, 257, SEEK_SET);
  79. msec=avio_rl16(pb);
  80. sec=avio_r8(pb);
  81. min=avio_rl32(pb);
  82. st->duration = av_rescale(1000*(min*60+sec)+msec, st->codec->sample_rate, 1000 * st->codec->frame_size);
  83. ctx->bytes_left_in_chunk=CHUNK_SIZE;
  84. avio_seek(pb, 512, SEEK_SET);
  85. return 0;
  86. }
  87. static int read_packet(AVFormatContext *s,
  88. AVPacket *pkt)
  89. {
  90. ACTContext *ctx = s->priv_data;
  91. AVIOContext *pb = s->pb;
  92. int ret;
  93. int frame_size=s->streams[0]->codec->sample_rate==8000?10:22;
  94. if(s->streams[0]->codec->sample_rate==8000)
  95. ret=av_new_packet(pkt, 10);
  96. else
  97. ret=av_new_packet(pkt, 11);
  98. if(ret)
  99. return ret;
  100. if(s->streams[0]->codec->sample_rate==4400 && !ctx->second_packet)
  101. {
  102. ret = avio_read(pb, ctx->audio_buffer, frame_size);
  103. if(ret<0)
  104. return ret;
  105. if(ret!=frame_size)
  106. return AVERROR(EIO);
  107. pkt->data[0]=ctx->audio_buffer[11];
  108. pkt->data[1]=ctx->audio_buffer[0];
  109. pkt->data[2]=ctx->audio_buffer[12];
  110. pkt->data[3]=ctx->audio_buffer[1];
  111. pkt->data[4]=ctx->audio_buffer[13];
  112. pkt->data[5]=ctx->audio_buffer[2];
  113. pkt->data[6]=ctx->audio_buffer[14];
  114. pkt->data[7]=ctx->audio_buffer[3];
  115. pkt->data[8]=ctx->audio_buffer[15];
  116. pkt->data[9]=ctx->audio_buffer[4];
  117. pkt->data[10]=ctx->audio_buffer[16];
  118. ctx->second_packet=1;
  119. }
  120. else if(s->streams[0]->codec->sample_rate==4400 && ctx->second_packet)
  121. {
  122. pkt->data[0]=ctx->audio_buffer[5];
  123. pkt->data[1]=ctx->audio_buffer[17];
  124. pkt->data[2]=ctx->audio_buffer[6];
  125. pkt->data[3]=ctx->audio_buffer[18];
  126. pkt->data[4]=ctx->audio_buffer[7];
  127. pkt->data[5]=ctx->audio_buffer[19];
  128. pkt->data[6]=ctx->audio_buffer[8];
  129. pkt->data[7]=ctx->audio_buffer[20];
  130. pkt->data[8]=ctx->audio_buffer[9];
  131. pkt->data[9]=ctx->audio_buffer[21];
  132. pkt->data[10]=ctx->audio_buffer[10];
  133. ctx->second_packet=0;
  134. }
  135. else // 8000 Hz
  136. {
  137. ret = avio_read(pb, ctx->audio_buffer, frame_size);
  138. if(ret<0)
  139. return ret;
  140. if(ret!=frame_size)
  141. return AVERROR(EIO);
  142. pkt->data[0]=ctx->audio_buffer[5];
  143. pkt->data[1]=ctx->audio_buffer[0];
  144. pkt->data[2]=ctx->audio_buffer[6];
  145. pkt->data[3]=ctx->audio_buffer[1];
  146. pkt->data[4]=ctx->audio_buffer[7];
  147. pkt->data[5]=ctx->audio_buffer[2];
  148. pkt->data[6]=ctx->audio_buffer[8];
  149. pkt->data[7]=ctx->audio_buffer[3];
  150. pkt->data[8]=ctx->audio_buffer[9];
  151. pkt->data[9]=ctx->audio_buffer[4];
  152. }
  153. ctx->bytes_left_in_chunk -= frame_size;
  154. if(ctx->bytes_left_in_chunk < frame_size)
  155. {
  156. avio_skip(pb, ctx->bytes_left_in_chunk);
  157. ctx->bytes_left_in_chunk=CHUNK_SIZE;
  158. }
  159. pkt->duration=1;
  160. return ret;
  161. }
  162. AVInputFormat ff_act_demuxer = {
  163. .name = "act",
  164. .long_name = "ACT Voice file format",
  165. .priv_data_size = sizeof(ACTContext),
  166. .read_probe = probe,
  167. .read_header = read_header,
  168. .read_packet = read_packet,
  169. };