qp_table.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * FFmpeg 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
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <stdint.h>
  19. #include "libavutil/frame.h"
  20. #include "libavutil/mem.h"
  21. #include "libavutil/video_enc_params.h"
  22. #include "qp_table.h"
  23. int ff_qp_table_extract(AVFrame *frame, int8_t **table, int *table_w, int *table_h,
  24. enum AVVideoEncParamsType *qscale_type)
  25. {
  26. AVFrameSideData *sd;
  27. AVVideoEncParams *par;
  28. unsigned int mb_h = (frame->height + 15) / 16;
  29. unsigned int mb_w = (frame->width + 15) / 16;
  30. unsigned int nb_mb = mb_h * mb_w;
  31. unsigned int block_idx;
  32. *table = NULL;
  33. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS);
  34. if (!sd)
  35. return 0;
  36. par = (AVVideoEncParams *)sd->data;
  37. if ((par->type != AV_VIDEO_ENC_PARAMS_MPEG2 &&
  38. par->type != AV_VIDEO_ENC_PARAMS_H264) ||
  39. (par->nb_blocks != 0 && par->nb_blocks != nb_mb))
  40. return AVERROR(ENOSYS);
  41. *table = av_malloc(nb_mb);
  42. if (!*table)
  43. return AVERROR(ENOMEM);
  44. if (table_w)
  45. *table_w = mb_w;
  46. if (table_h)
  47. *table_h = mb_h;
  48. if (qscale_type)
  49. *qscale_type = par->type;
  50. if (par->nb_blocks == 0) {
  51. memset(*table, par->qp, nb_mb);
  52. return 0;
  53. }
  54. for (block_idx = 0; block_idx < nb_mb; block_idx++) {
  55. AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx);
  56. (*table)[block_idx] = par->qp + b->delta_qp;
  57. }
  58. return 0;
  59. }