bitstream_filter.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "avcodec.h"
  19. AVBitStreamFilter *first_bitstream_filter= NULL;
  20. void av_register_bitstream_filter(AVBitStreamFilter *bsf){
  21. bsf->next = first_bitstream_filter;
  22. first_bitstream_filter= bsf;
  23. }
  24. AVBitStreamFilterContext *av_bitstream_filter_init(const char *name){
  25. AVBitStreamFilter *bsf= first_bitstream_filter;
  26. while(bsf){
  27. if(!strcmp(name, bsf->name)){
  28. AVBitStreamFilterContext *bsfc= av_mallocz(sizeof(AVBitStreamFilterContext));
  29. bsfc->filter= bsf;
  30. bsfc->priv_data= av_mallocz(bsf->priv_data_size);
  31. return bsfc;
  32. }
  33. bsf= bsf->next;
  34. }
  35. return NULL;
  36. }
  37. void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc){
  38. av_freep(&bsfc->priv_data);
  39. av_parser_close(bsfc->parser);
  40. av_free(bsfc);
  41. }
  42. int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
  43. AVCodecContext *avctx, const char *args,
  44. uint8_t **poutbuf, int *poutbuf_size,
  45. const uint8_t *buf, int buf_size, int keyframe){
  46. *poutbuf= (uint8_t *) buf;
  47. *poutbuf_size= buf_size;
  48. return bsfc->filter->filter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, buf_size, keyframe);
  49. }
  50. static int dump_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  51. uint8_t **poutbuf, int *poutbuf_size,
  52. const uint8_t *buf, int buf_size, int keyframe){
  53. int cmd= args ? *args : 0;
  54. /* cast to avoid warning about discarding qualifiers */
  55. if(avctx->extradata){
  56. if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER) && cmd=='a')
  57. ||(keyframe && (cmd=='k' || !cmd))
  58. ||(cmd=='e')
  59. /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
  60. int size= buf_size + avctx->extradata_size;
  61. *poutbuf_size= size;
  62. *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  63. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  64. memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  65. return 1;
  66. }
  67. }
  68. return 0;
  69. }
  70. static int remove_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  71. uint8_t **poutbuf, int *poutbuf_size,
  72. const uint8_t *buf, int buf_size, int keyframe){
  73. int cmd= args ? *args : 0;
  74. AVCodecParserContext *s;
  75. if(!bsfc->parser){
  76. bsfc->parser= av_parser_init(avctx->codec_id);
  77. }
  78. s= bsfc->parser;
  79. if(s && s->parser->split){
  80. if( (((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) && cmd=='a')
  81. ||(!keyframe && cmd=='k')
  82. ||(cmd=='e' || !cmd)
  83. ){
  84. int i= s->parser->split(avctx, buf, buf_size);
  85. buf += i;
  86. buf_size -= i;
  87. }
  88. }
  89. *poutbuf= (uint8_t *) buf;
  90. *poutbuf_size= buf_size;
  91. return 0;
  92. }
  93. static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  94. uint8_t **poutbuf, int *poutbuf_size,
  95. const uint8_t *buf, int buf_size, int keyframe){
  96. int amount= args ? atoi(args) : 10000;
  97. unsigned int *state= bsfc->priv_data;
  98. int i;
  99. *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  100. memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  101. for(i=0; i<buf_size; i++){
  102. (*state) += (*poutbuf)[i] + 1;
  103. if(*state % amount == 0)
  104. (*poutbuf)[i] = *state;
  105. }
  106. return 1;
  107. }
  108. AVBitStreamFilter dump_extradata_bsf={
  109. "dump_extra",
  110. 0,
  111. dump_extradata,
  112. };
  113. AVBitStreamFilter remove_extradata_bsf={
  114. "remove_extra",
  115. 0,
  116. remove_extradata,
  117. };
  118. AVBitStreamFilter noise_bsf={
  119. "noise",
  120. sizeof(int),
  121. noise,
  122. };