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.

263 lines
8.2KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avcodec.h"
  21. #include "mpegaudio.h"
  22. AVBitStreamFilter *first_bitstream_filter= NULL;
  23. void av_register_bitstream_filter(AVBitStreamFilter *bsf){
  24. bsf->next = first_bitstream_filter;
  25. first_bitstream_filter= bsf;
  26. }
  27. AVBitStreamFilterContext *av_bitstream_filter_init(const char *name){
  28. AVBitStreamFilter *bsf= first_bitstream_filter;
  29. while(bsf){
  30. if(!strcmp(name, bsf->name)){
  31. AVBitStreamFilterContext *bsfc= av_mallocz(sizeof(AVBitStreamFilterContext));
  32. bsfc->filter= bsf;
  33. bsfc->priv_data= av_mallocz(bsf->priv_data_size);
  34. return bsfc;
  35. }
  36. bsf= bsf->next;
  37. }
  38. return NULL;
  39. }
  40. void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc){
  41. av_freep(&bsfc->priv_data);
  42. av_parser_close(bsfc->parser);
  43. av_free(bsfc);
  44. }
  45. int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
  46. AVCodecContext *avctx, const char *args,
  47. uint8_t **poutbuf, int *poutbuf_size,
  48. const uint8_t *buf, int buf_size, int keyframe){
  49. *poutbuf= (uint8_t *) buf;
  50. *poutbuf_size= buf_size;
  51. return bsfc->filter->filter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, buf_size, keyframe);
  52. }
  53. static int dump_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  54. uint8_t **poutbuf, int *poutbuf_size,
  55. const uint8_t *buf, int buf_size, int keyframe){
  56. int cmd= args ? *args : 0;
  57. /* cast to avoid warning about discarding qualifiers */
  58. if(avctx->extradata){
  59. if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER) && cmd=='a')
  60. ||(keyframe && (cmd=='k' || !cmd))
  61. ||(cmd=='e')
  62. /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
  63. int size= buf_size + avctx->extradata_size;
  64. *poutbuf_size= size;
  65. *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  66. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  67. memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  68. return 1;
  69. }
  70. }
  71. return 0;
  72. }
  73. static int remove_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  74. uint8_t **poutbuf, int *poutbuf_size,
  75. const uint8_t *buf, int buf_size, int keyframe){
  76. int cmd= args ? *args : 0;
  77. AVCodecParserContext *s;
  78. if(!bsfc->parser){
  79. bsfc->parser= av_parser_init(avctx->codec_id);
  80. }
  81. s= bsfc->parser;
  82. if(s && s->parser->split){
  83. if( (((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) && cmd=='a')
  84. ||(!keyframe && cmd=='k')
  85. ||(cmd=='e' || !cmd)
  86. ){
  87. int i= s->parser->split(avctx, buf, buf_size);
  88. buf += i;
  89. buf_size -= i;
  90. }
  91. }
  92. *poutbuf= (uint8_t *) buf;
  93. *poutbuf_size= buf_size;
  94. return 0;
  95. }
  96. static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  97. uint8_t **poutbuf, int *poutbuf_size,
  98. const uint8_t *buf, int buf_size, int keyframe){
  99. int amount= args ? atoi(args) : 10000;
  100. unsigned int *state= bsfc->priv_data;
  101. int i;
  102. *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  103. memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  104. for(i=0; i<buf_size; i++){
  105. (*state) += (*poutbuf)[i] + 1;
  106. if(*state % amount == 0)
  107. (*poutbuf)[i] = *state;
  108. }
  109. return 1;
  110. }
  111. static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  112. uint8_t **poutbuf, int *poutbuf_size,
  113. const uint8_t *buf, int buf_size, int keyframe){
  114. uint32_t header;
  115. int mode_extension;
  116. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  117. av_log(avctx, AV_LOG_ERROR, "not standards compliant\n");
  118. return -1;
  119. }
  120. header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  121. mode_extension= (header>>4)&3;
  122. if(ff_mpa_check_header(header) < 0 || (header&0x70000) != 0x30000){
  123. *poutbuf= (uint8_t *) buf;
  124. *poutbuf_size= buf_size;
  125. av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header);
  126. return 0;
  127. }
  128. *poutbuf_size= buf_size - 4;
  129. *poutbuf= av_malloc(buf_size - 4 + FF_INPUT_BUFFER_PADDING_SIZE);
  130. memcpy(*poutbuf, buf + 4, buf_size - 4 + FF_INPUT_BUFFER_PADDING_SIZE);
  131. if(avctx->channels==2){
  132. if((header & (3<<19)) != 3<<19){
  133. (*poutbuf)[1] &= 0x3F;
  134. (*poutbuf)[1] |= mode_extension<<6;
  135. FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);
  136. }else{
  137. (*poutbuf)[1] &= 0x8F;
  138. (*poutbuf)[1] |= mode_extension<<4;
  139. }
  140. }
  141. return 1;
  142. }
  143. static int mp3_header_decompress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  144. uint8_t **poutbuf, int *poutbuf_size,
  145. const uint8_t *buf, int buf_size, int keyframe){
  146. uint32_t header;
  147. int sample_rate= avctx->sample_rate;
  148. int sample_rate_index=0;
  149. int lsf, mpeg25, bitrate_index, frame_size;
  150. header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  151. if(ff_mpa_check_header(header) >= 0){
  152. *poutbuf= (uint8_t *) buf;
  153. *poutbuf_size= buf_size;
  154. return 0;
  155. }
  156. header= 0xFFE00000 | ((4-3)<<17) | (1<<16); //FIXME simplify
  157. lsf = sample_rate < (24000+32000)/2;
  158. mpeg25 = sample_rate < (12000+16000)/2;
  159. header |= (!mpeg25)<<20;
  160. header |= (!lsf )<<19;
  161. if(sample_rate<<(lsf+mpeg25) < (44100+32000)/2)
  162. sample_rate_index |= 2;
  163. else if(sample_rate<<(lsf+mpeg25) > (44100+48000)/2)
  164. sample_rate_index |= 1;
  165. header |= sample_rate_index<<10;
  166. sample_rate= mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); //in case sample rate is a little off
  167. for(bitrate_index=2; bitrate_index<30; bitrate_index++){
  168. frame_size = mpa_bitrate_tab[lsf][2][bitrate_index>>1];
  169. frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1);
  170. if(frame_size == buf_size + 4)
  171. break;
  172. }
  173. if(bitrate_index == 30){
  174. av_log(avctx, AV_LOG_ERROR, "couldnt find bitrate_index\n");
  175. return -1;
  176. }
  177. header |= (bitrate_index&1)<<9;
  178. header |= (bitrate_index>>1)<<12;
  179. header |= (avctx->channels==1 ? MPA_MONO : MPA_JSTEREO)<<6;
  180. *poutbuf_size= buf_size + 4;
  181. *poutbuf= av_malloc(buf_size + 4 + FF_INPUT_BUFFER_PADDING_SIZE);
  182. memcpy(*poutbuf + 4, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  183. if(avctx->channels==2){
  184. if(lsf){
  185. FFSWAP(int, (*poutbuf)[5], (*poutbuf)[6]);
  186. header |= ((*poutbuf)[5] & 0xC0)>>2;
  187. }else{
  188. header |= (*poutbuf)[5] & 0x30;
  189. }
  190. }
  191. (*poutbuf)[0]= header>>24;
  192. (*poutbuf)[1]= header>>16;
  193. (*poutbuf)[2]= header>> 8;
  194. (*poutbuf)[3]= header ;
  195. return 1;
  196. }
  197. AVBitStreamFilter dump_extradata_bsf={
  198. "dump_extra",
  199. 0,
  200. dump_extradata,
  201. };
  202. AVBitStreamFilter remove_extradata_bsf={
  203. "remove_extra",
  204. 0,
  205. remove_extradata,
  206. };
  207. AVBitStreamFilter noise_bsf={
  208. "noise",
  209. sizeof(int),
  210. noise,
  211. };
  212. AVBitStreamFilter mp3_header_compress_bsf={
  213. "mp3comp",
  214. 0,
  215. mp3_header_compress,
  216. };
  217. AVBitStreamFilter mp3_header_decompress_bsf={
  218. "mp3decomp",
  219. 0,
  220. mp3_header_decompress,
  221. };