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.

91 lines
2.4KB

  1. #include "libavutil/opt.h"
  2. #include "libavutil/samplefmt.h"
  3. #include "avcodec.h"
  4. #include "ac3.h"
  5. typedef struct CombineContext{
  6. AVClass *av_class; ///< AVClass used for AVOption
  7. AC3EncOptions options; ///< encoding options
  8. void *ctx;
  9. AVCodec *codec;
  10. }CombineContext;
  11. static AVClass ac3enc_class = { "AC-3 Encoder", av_default_item_name,
  12. ff_ac3_options, LIBAVUTIL_VERSION_INT };
  13. static av_cold AVCodec *get_codec(enum AVSampleFormat s){
  14. #if CONFIG_AC3_FIXED_ENCODER
  15. if(s==AV_SAMPLE_FMT_S16) return &ff_ac3_fixed_encoder;
  16. #endif
  17. #if CONFIG_AC3_FLOAT_ENCODER
  18. if(s==AV_SAMPLE_FMT_FLT) return &ff_ac3_float_encoder;
  19. #endif
  20. return NULL;
  21. }
  22. static av_cold int encode_init(AVCodecContext *avctx)
  23. {
  24. CombineContext *c= avctx->priv_data;
  25. int ret;
  26. int offset= (uint8_t*)&c->options - (uint8_t*)c;
  27. c->codec= get_codec(avctx->sample_fmt);
  28. if(!c->codec){
  29. av_log(avctx, AV_LOG_ERROR, "Unsupported sample format\n");
  30. return -1;
  31. }
  32. c->ctx= av_mallocz(c->codec->priv_data_size);
  33. memcpy((uint8_t*)c->ctx + offset, &c->options, (uint8_t*)&c->ctx - (uint8_t*)&c->options);
  34. FFSWAP(void *,avctx->priv_data, c->ctx);
  35. ret= c->codec->init(avctx);
  36. FFSWAP(void *,avctx->priv_data, c->ctx);
  37. return ret;
  38. }
  39. static int encode_frame(AVCodecContext *avctx, unsigned char *frame,
  40. int buf_size, void *data)
  41. {
  42. CombineContext *c= avctx->priv_data;
  43. int ret;
  44. FFSWAP(void *,avctx->priv_data, c->ctx);
  45. ret= c->codec->encode(avctx, frame, buf_size, data);
  46. FFSWAP(void *,avctx->priv_data, c->ctx);
  47. return ret;
  48. }
  49. static av_cold int encode_close(AVCodecContext *avctx)
  50. {
  51. CombineContext *c= avctx->priv_data;
  52. int ret;
  53. FFSWAP(void *,avctx->priv_data, c->ctx);
  54. ret= c->codec->close(avctx);
  55. FFSWAP(void *,avctx->priv_data, c->ctx);
  56. return ret;
  57. }
  58. AVCodec ff_ac3_encoder = {
  59. "ac3",
  60. AVMEDIA_TYPE_AUDIO,
  61. CODEC_ID_AC3,
  62. sizeof(CombineContext),
  63. encode_init,
  64. encode_frame,
  65. encode_close,
  66. NULL,
  67. .sample_fmts = (const enum AVSampleFormat[]){
  68. #if CONFIG_AC3_FLOAT_ENCODER
  69. AV_SAMPLE_FMT_FLT,
  70. #endif
  71. #if CONFIG_AC3_FIXED_ENCODER
  72. AV_SAMPLE_FMT_S16,
  73. #endif
  74. AV_SAMPLE_FMT_NONE},
  75. .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
  76. .priv_class = &ac3enc_class,
  77. .channel_layouts = ff_ac3_channel_layouts,
  78. };