bitstream_filter.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #define MP3_MASK 0xFFFE0CCF
  112. static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  113. uint8_t **poutbuf, int *poutbuf_size,
  114. const uint8_t *buf, int buf_size, int keyframe){
  115. uint32_t header, extraheader;
  116. int mode_extension, header_size;
  117. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  118. av_log(avctx, AV_LOG_ERROR, "not standards compliant\n");
  119. return -1;
  120. }
  121. header = AV_RB32(buf);
  122. mode_extension= (header>>4)&3;
  123. if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){
  124. output_unchanged:
  125. *poutbuf= (uint8_t *) buf;
  126. *poutbuf_size= buf_size;
  127. av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header);
  128. return 0;
  129. }
  130. if(avctx->extradata_size == 0){
  131. avctx->extradata_size=15;
  132. avctx->extradata= av_malloc(avctx->extradata_size);
  133. strcpy(avctx->extradata, "FFCMP3 0.0");
  134. memcpy(avctx->extradata+11, buf, 4);
  135. }
  136. if(avctx->extradata_size != 15){
  137. av_log(avctx, AV_LOG_ERROR, "Extradata invalid\n");
  138. return -1;
  139. }
  140. extraheader = AV_RB32(avctx->extradata+11);
  141. if((extraheader&MP3_MASK) != (header&MP3_MASK))
  142. goto output_unchanged;
  143. header_size= (header&0x10000) ? 4 : 6;
  144. *poutbuf_size= buf_size - header_size;
  145. *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
  146. memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
  147. if(avctx->channels==2){
  148. if((header & (3<<19)) != 3<<19){
  149. (*poutbuf)[1] &= 0x3F;
  150. (*poutbuf)[1] |= mode_extension<<6;
  151. FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);
  152. }else{
  153. (*poutbuf)[1] &= 0x8F;
  154. (*poutbuf)[1] |= mode_extension<<4;
  155. }
  156. }
  157. return 1;
  158. }
  159. static int mp3_header_decompress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  160. uint8_t **poutbuf, int *poutbuf_size,
  161. const uint8_t *buf, int buf_size, int keyframe){
  162. uint32_t header;
  163. int sample_rate= avctx->sample_rate;
  164. int sample_rate_index=0;
  165. int lsf, mpeg25, bitrate_index, frame_size;
  166. header = AV_RB32(buf);
  167. if(ff_mpa_check_header(header) >= 0){
  168. *poutbuf= (uint8_t *) buf;
  169. *poutbuf_size= buf_size;
  170. return 0;
  171. }
  172. if(avctx->extradata_size != 15 || strcmp(avctx->extradata, "FFCMP3 0.0")){
  173. av_log(avctx, AV_LOG_ERROR, "Extradata invalid %d\n", avctx->extradata_size);
  174. return -1;
  175. }
  176. header= AV_RB32(avctx->extradata+11) & MP3_MASK;
  177. lsf = sample_rate < (24000+32000)/2;
  178. mpeg25 = sample_rate < (12000+16000)/2;
  179. sample_rate_index= (header>>10)&3;
  180. sample_rate= mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); //in case sample rate is a little off
  181. for(bitrate_index=2; bitrate_index<30; bitrate_index++){
  182. frame_size = mpa_bitrate_tab[lsf][2][bitrate_index>>1];
  183. frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1);
  184. if(frame_size == buf_size + 4)
  185. break;
  186. if(frame_size == buf_size + 6)
  187. break;
  188. }
  189. if(bitrate_index == 30){
  190. av_log(avctx, AV_LOG_ERROR, "couldnt find bitrate_index\n");
  191. return -1;
  192. }
  193. header |= (bitrate_index&1)<<9;
  194. header |= (bitrate_index>>1)<<12;
  195. header |= (frame_size == buf_size + 4)<<16; //FIXME actually set a correct crc instead of 0
  196. *poutbuf_size= frame_size;
  197. *poutbuf= av_malloc(frame_size + FF_INPUT_BUFFER_PADDING_SIZE);
  198. memcpy(*poutbuf + frame_size - buf_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  199. if(avctx->channels==2){
  200. uint8_t *p= *poutbuf + frame_size - buf_size;
  201. if(lsf){
  202. FFSWAP(int, p[1], p[2]);
  203. header |= (p[1] & 0xC0)>>2;
  204. p[1] &= 0x3F;
  205. }else{
  206. header |= p[1] & 0x30;
  207. p[1] &= 0xCF;
  208. }
  209. }
  210. (*poutbuf)[0]= header>>24;
  211. (*poutbuf)[1]= header>>16;
  212. (*poutbuf)[2]= header>> 8;
  213. (*poutbuf)[3]= header ;
  214. return 1;
  215. }
  216. AVBitStreamFilter dump_extradata_bsf={
  217. "dump_extra",
  218. 0,
  219. dump_extradata,
  220. };
  221. AVBitStreamFilter remove_extradata_bsf={
  222. "remove_extra",
  223. 0,
  224. remove_extradata,
  225. };
  226. AVBitStreamFilter noise_bsf={
  227. "noise",
  228. sizeof(int),
  229. noise,
  230. };
  231. AVBitStreamFilter mp3_header_compress_bsf={
  232. "mp3comp",
  233. 0,
  234. mp3_header_compress,
  235. };
  236. AVBitStreamFilter mp3_header_decompress_bsf={
  237. "mp3decomp",
  238. 0,
  239. mp3_header_decompress,
  240. };