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.

97 lines
3.0KB

  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. return bsfc;
  14. }
  15. bsf= bsf->next;
  16. }
  17. return NULL;
  18. }
  19. void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc){
  20. av_parser_close(bsfc->parser);
  21. av_free(bsfc);
  22. }
  23. int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
  24. AVCodecContext *avctx, const char *args,
  25. uint8_t **poutbuf, int *poutbuf_size,
  26. const uint8_t *buf, int buf_size, int keyframe){
  27. return bsfc->filter->filter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, buf_size, keyframe);
  28. }
  29. static int dump_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  30. uint8_t **poutbuf, int *poutbuf_size,
  31. const uint8_t *buf, int buf_size, int keyframe){
  32. int cmd= args ? *args : 0;
  33. /* cast to avoid warning about discarding qualifiers */
  34. *poutbuf= (uint8_t *) buf;
  35. *poutbuf_size= buf_size;
  36. if(avctx->extradata){
  37. if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER) && cmd=='a')
  38. ||(keyframe && (cmd=='k' || !cmd))
  39. ||(cmd=='e')
  40. /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
  41. int size= buf_size + avctx->extradata_size;
  42. *poutbuf_size= size;
  43. *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  44. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  45. memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  46. return 1;
  47. }
  48. }
  49. return 0;
  50. }
  51. static int remove_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  52. uint8_t **poutbuf, int *poutbuf_size,
  53. const uint8_t *buf, int buf_size, int keyframe){
  54. int cmd= args ? *args : 0;
  55. AVCodecParserContext *s;
  56. if(!bsfc->parser){
  57. bsfc->parser= av_parser_init(avctx->codec_id);
  58. }
  59. s= bsfc->parser;
  60. if(s && s->parser->split){
  61. if( (((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) && cmd=='a')
  62. ||(!keyframe && cmd=='k')
  63. ||(cmd=='e' || !cmd)
  64. ){
  65. int i= s->parser->split(avctx, buf, buf_size);
  66. buf += i;
  67. buf_size -= i;
  68. }
  69. }
  70. *poutbuf= (uint8_t *) buf;
  71. *poutbuf_size= buf_size;
  72. return 0;
  73. }
  74. AVBitStreamFilter dump_extradata_bsf={
  75. "dump_extra",
  76. dump_extradata,
  77. };
  78. AVBitStreamFilter remove_extradata_bsf={
  79. "remove_extra",
  80. remove_extradata,
  81. };