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.

125 lines
3.8KB

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