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.

144 lines
4.6KB

  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. AVBitStreamFilter *first_bitstream_filter= NULL;
  22. void av_register_bitstream_filter(AVBitStreamFilter *bsf){
  23. bsf->next = first_bitstream_filter;
  24. first_bitstream_filter= bsf;
  25. }
  26. AVBitStreamFilterContext *av_bitstream_filter_init(const char *name){
  27. AVBitStreamFilter *bsf= first_bitstream_filter;
  28. while(bsf){
  29. if(!strcmp(name, bsf->name)){
  30. AVBitStreamFilterContext *bsfc= av_mallocz(sizeof(AVBitStreamFilterContext));
  31. bsfc->filter= bsf;
  32. bsfc->priv_data= av_mallocz(bsf->priv_data_size);
  33. return bsfc;
  34. }
  35. bsf= bsf->next;
  36. }
  37. return NULL;
  38. }
  39. void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc){
  40. av_freep(&bsfc->priv_data);
  41. av_parser_close(bsfc->parser);
  42. av_free(bsfc);
  43. }
  44. int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
  45. AVCodecContext *avctx, const char *args,
  46. uint8_t **poutbuf, int *poutbuf_size,
  47. const uint8_t *buf, int buf_size, int keyframe){
  48. *poutbuf= (uint8_t *) buf;
  49. *poutbuf_size= buf_size;
  50. return bsfc->filter->filter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, buf_size, keyframe);
  51. }
  52. static int dump_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  53. uint8_t **poutbuf, int *poutbuf_size,
  54. const uint8_t *buf, int buf_size, int keyframe){
  55. int cmd= args ? *args : 0;
  56. /* cast to avoid warning about discarding qualifiers */
  57. if(avctx->extradata){
  58. if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER) && cmd=='a')
  59. ||(keyframe && (cmd=='k' || !cmd))
  60. ||(cmd=='e')
  61. /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
  62. int size= buf_size + avctx->extradata_size;
  63. *poutbuf_size= size;
  64. *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  65. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  66. memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  67. return 1;
  68. }
  69. }
  70. return 0;
  71. }
  72. static int remove_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  73. uint8_t **poutbuf, int *poutbuf_size,
  74. const uint8_t *buf, int buf_size, int keyframe){
  75. int cmd= args ? *args : 0;
  76. AVCodecParserContext *s;
  77. if(!bsfc->parser){
  78. bsfc->parser= av_parser_init(avctx->codec_id);
  79. }
  80. s= bsfc->parser;
  81. if(s && s->parser->split){
  82. if( (((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) && cmd=='a')
  83. ||(!keyframe && cmd=='k')
  84. ||(cmd=='e' || !cmd)
  85. ){
  86. int i= s->parser->split(avctx, buf, buf_size);
  87. buf += i;
  88. buf_size -= i;
  89. }
  90. }
  91. *poutbuf= (uint8_t *) buf;
  92. *poutbuf_size= buf_size;
  93. return 0;
  94. }
  95. static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  96. uint8_t **poutbuf, int *poutbuf_size,
  97. const uint8_t *buf, int buf_size, int keyframe){
  98. int amount= args ? atoi(args) : 10000;
  99. unsigned int *state= bsfc->priv_data;
  100. int i;
  101. *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  102. memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  103. for(i=0; i<buf_size; i++){
  104. (*state) += (*poutbuf)[i] + 1;
  105. if(*state % amount == 0)
  106. (*poutbuf)[i] = *state;
  107. }
  108. return 1;
  109. }
  110. AVBitStreamFilter dump_extradata_bsf={
  111. "dump_extra",
  112. 0,
  113. dump_extradata,
  114. };
  115. AVBitStreamFilter remove_extradata_bsf={
  116. "remove_extra",
  117. 0,
  118. remove_extradata,
  119. };
  120. AVBitStreamFilter noise_bsf={
  121. "noise",
  122. sizeof(int),
  123. noise,
  124. };