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.

294 lines
9.9KB

  1. /*
  2. * AVS2 encoding using the xavs2 library
  3. *
  4. * Copyright (C) 2018 Yiqun Xu, <yiqun.xu@vipl.ict.ac.cn>
  5. * Falei Luo, <falei.luo@gmail.com>
  6. * Huiwen Ren, <hwrenx@gmail.com>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "xavs2.h"
  25. #include "mpeg12.h"
  26. #include "libavutil/avstring.h"
  27. #define xavs2_opt_set2(name, format, ...) do{ \
  28. char opt_str[16] = {0}; \
  29. int err; \
  30. av_strlcatf(opt_str, sizeof(opt_str), format, __VA_ARGS__); \
  31. err = cae->api->opt_set2(cae->param, name, opt_str); \
  32. if (err) {\
  33. av_log(avctx, AV_LOG_WARNING, "Invalid value for %s: %s\n", name, opt_str);\
  34. }\
  35. } while(0);
  36. typedef struct XAVS2EContext {
  37. AVClass *class;
  38. int lcu_row_threads;
  39. int initial_qp;
  40. int qp;
  41. int max_qp;
  42. int min_qp;
  43. int preset_level;
  44. int log_level;
  45. int hierarchical_reference;
  46. void *encoder;
  47. char *xavs2_opts;
  48. xavs2_outpacket_t packet;
  49. xavs2_param_t *param;
  50. const xavs2_api_t *api;
  51. } XAVS2EContext;
  52. static av_cold int xavs2_init(AVCodecContext *avctx)
  53. {
  54. XAVS2EContext *cae= avctx->priv_data;
  55. int bit_depth, code;
  56. bit_depth = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 8 : 10;
  57. /* get API handler */
  58. cae->api = xavs2_api_get(bit_depth);
  59. if (!cae->api) {
  60. av_log(avctx, AV_LOG_ERROR, "api get failed\n");
  61. return AVERROR_EXTERNAL;
  62. }
  63. cae->param = cae->api->opt_alloc();
  64. if (!cae->param) {
  65. av_log(avctx, AV_LOG_ERROR, "param alloc failed\n");
  66. return AVERROR(ENOMEM);
  67. }
  68. xavs2_opt_set2("width", "%d", avctx->width);
  69. xavs2_opt_set2("height", "%d", avctx->height);
  70. xavs2_opt_set2("bframes", "%d", avctx->max_b_frames);
  71. xavs2_opt_set2("bitdepth", "%d", bit_depth);
  72. xavs2_opt_set2("log", "%d", cae->log_level);
  73. xavs2_opt_set2("preset", "%d", cae->preset_level);
  74. /* not the same parameter as the IntraPeriod in xavs2 log */
  75. xavs2_opt_set2("intraperiod", "%d", avctx->gop_size);
  76. xavs2_opt_set2("thread_frames", "%d", avctx->thread_count);
  77. xavs2_opt_set2("thread_rows", "%d", cae->lcu_row_threads);
  78. xavs2_opt_set2("OpenGOP", "%d", 1);
  79. if (cae->xavs2_opts) {
  80. AVDictionary *dict = NULL;
  81. AVDictionaryEntry *en = NULL;
  82. if (!av_dict_parse_string(&dict, cae->xavs2_opts, "=", ":", 0)) {
  83. while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
  84. xavs2_opt_set2(en->key, "%s", en->value);
  85. }
  86. av_dict_free(&dict);
  87. }
  88. }
  89. /* Rate control */
  90. if (avctx->bit_rate > 0) {
  91. xavs2_opt_set2("RateControl", "%d", 1);
  92. xavs2_opt_set2("TargetBitRate", "%"PRId64"", avctx->bit_rate);
  93. xavs2_opt_set2("initial_qp", "%d", cae->initial_qp);
  94. xavs2_opt_set2("max_qp", "%d", cae->max_qp);
  95. xavs2_opt_set2("min_qp", "%d", cae->min_qp);
  96. } else {
  97. xavs2_opt_set2("initial_qp", "%d", cae->qp);
  98. }
  99. ff_mpeg12_find_best_frame_rate(avctx->framerate, &code, NULL, NULL, 0);
  100. xavs2_opt_set2("FrameRate", "%d", code);
  101. cae->encoder = cae->api->encoder_create(cae->param);
  102. if (!cae->encoder) {
  103. av_log(avctx,AV_LOG_ERROR, "Can not create encoder. Null pointer returned\n");
  104. return AVERROR(EINVAL);
  105. }
  106. return 0;
  107. }
  108. static void xavs2_copy_frame_with_shift(xavs2_picture_t *pic, const AVFrame *frame, const int shift_in)
  109. {
  110. int j, k;
  111. for (k = 0; k < 3; k++) {
  112. int i_stride = pic->img.i_stride[k];
  113. for (j = 0; j < pic->img.i_lines[k]; j++) {
  114. uint16_t *p_plane = (uint16_t *)&pic->img.img_planes[k][j * i_stride];
  115. int i;
  116. uint8_t *p_buffer = frame->data[k] + frame->linesize[k] * j;
  117. memset(p_plane, 0, i_stride);
  118. for (i = 0; i < pic->img.i_width[k]; i++) {
  119. p_plane[i] = p_buffer[i] << shift_in;
  120. }
  121. }
  122. }
  123. }
  124. static void xavs2_copy_frame(xavs2_picture_t *pic, const AVFrame *frame)
  125. {
  126. int j, k;
  127. for (k = 0; k < 3; k++) {
  128. for (j = 0; j < pic->img.i_lines[k]; j++) {
  129. memcpy( pic->img.img_planes[k] + pic->img.i_stride[k] * j,
  130. frame->data[k]+frame->linesize[k] * j,
  131. pic->img.i_width[k] * pic->img.in_sample_size);
  132. }
  133. }
  134. }
  135. static int xavs2_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  136. const AVFrame *frame, int *got_packet)
  137. {
  138. XAVS2EContext *cae = avctx->priv_data;
  139. xavs2_picture_t pic;
  140. int ret;
  141. /* create the XAVS2 video encoder */
  142. /* read frame data and send to the XAVS2 video encoder */
  143. if (cae->api->encoder_get_buffer(cae->encoder, &pic) < 0) {
  144. av_log(avctx,AV_LOG_ERROR, "failed to get frame buffer\n");
  145. return AVERROR_EXTERNAL;
  146. }
  147. if (frame) {
  148. switch (frame->format) {
  149. case AV_PIX_FMT_YUV420P:
  150. if (pic.img.in_sample_size == pic.img.enc_sample_size) {
  151. xavs2_copy_frame(&pic, frame);
  152. } else {
  153. const int shift_in = atoi(cae->api->opt_get(cae->param, "SampleShift"));
  154. xavs2_copy_frame_with_shift(&pic, frame, shift_in);
  155. }
  156. break;
  157. case AV_PIX_FMT_YUV420P10:
  158. if (pic.img.in_sample_size == pic.img.enc_sample_size) {
  159. xavs2_copy_frame(&pic, frame);
  160. break;
  161. }
  162. default:
  163. av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
  164. return AVERROR(EINVAL);
  165. break;
  166. }
  167. pic.i_state = 0;
  168. pic.i_pts = frame->pts;
  169. pic.i_type = XAVS2_TYPE_AUTO;
  170. ret = cae->api->encoder_encode(cae->encoder, &pic, &cae->packet);
  171. if (ret) {
  172. av_log(avctx, AV_LOG_ERROR, "encode failed\n");
  173. return AVERROR_EXTERNAL;
  174. }
  175. } else {
  176. cae->api->encoder_encode(cae->encoder, NULL, &cae->packet);
  177. }
  178. if ((cae->packet.len) && (cae->packet.state != XAVS2_STATE_FLUSH_END)){
  179. if (av_new_packet(pkt, cae->packet.len) < 0){
  180. av_log(avctx, AV_LOG_ERROR, "packet alloc failed\n");
  181. cae->api->encoder_packet_unref(cae->encoder, &cae->packet);
  182. return AVERROR(ENOMEM);
  183. }
  184. pkt->pts = cae->packet.pts;
  185. pkt->dts = cae->packet.dts;
  186. memcpy(pkt->data, cae->packet.stream, cae->packet.len);
  187. pkt->size = cae->packet.len;
  188. cae->api->encoder_packet_unref(cae->encoder, &cae->packet);
  189. *got_packet = 1;
  190. } else {
  191. *got_packet = 0;
  192. }
  193. return 0;
  194. }
  195. static av_cold int xavs2_close(AVCodecContext *avctx)
  196. {
  197. XAVS2EContext *cae = avctx->priv_data;
  198. /* destroy the encoder */
  199. if (cae->api) {
  200. cae->api->encoder_destroy(cae->encoder);
  201. if (cae->param) {
  202. cae->api->opt_destroy(cae->param);
  203. }
  204. }
  205. return 0;
  206. }
  207. #define OFFSET(x) offsetof(XAVS2EContext, x)
  208. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  209. static const AVOption options[] = {
  210. { "lcu_row_threads" , "number of parallel threads for rows" , OFFSET(lcu_row_threads) , AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, VE },
  211. { "initial_qp" , "Quantization initial parameter" , OFFSET(initial_qp) , AV_OPT_TYPE_INT, {.i64 = 34 }, 1, 63, VE },
  212. { "qp" , "Quantization parameter" , OFFSET(qp) , AV_OPT_TYPE_INT, {.i64 = 34 }, 1, 63, VE },
  213. { "max_qp" , "max qp for rate control" , OFFSET(max_qp) , AV_OPT_TYPE_INT, {.i64 = 55 }, 0, 63, VE },
  214. { "min_qp" , "min qp for rate control" , OFFSET(min_qp) , AV_OPT_TYPE_INT, {.i64 = 20 }, 0, 63, VE },
  215. { "speed_level" , "Speed level, higher is better but slower", OFFSET(preset_level) , AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 9, VE },
  216. { "log_level" , "log level: -1: none, 0: error, 1: warning, 2: info, 3: debug", OFFSET(log_level) , AV_OPT_TYPE_INT, {.i64 = 0 }, -1, 3, VE },
  217. { "xavs2-params" , "set the xavs2 configuration using a :-separated list of key=value parameters", OFFSET(xavs2_opts), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  218. { NULL },
  219. };
  220. static const AVClass libxavs2 = {
  221. .class_name = "XAVS2EContext",
  222. .item_name = av_default_item_name,
  223. .option = options,
  224. .version = LIBAVUTIL_VERSION_INT,
  225. };
  226. static const AVCodecDefault xavs2_defaults[] = {
  227. { "b", "0" },
  228. { "g", "48" },
  229. { "bf", "7" },
  230. { NULL },
  231. };
  232. AVCodec ff_libxavs2_encoder = {
  233. .name = "libxavs2",
  234. .long_name = NULL_IF_CONFIG_SMALL("libxavs2 AVS2-P2/IEEE1857.4"),
  235. .type = AVMEDIA_TYPE_VIDEO,
  236. .id = AV_CODEC_ID_AVS2,
  237. .priv_data_size = sizeof(XAVS2EContext),
  238. .init = xavs2_init,
  239. .encode2 = xavs2_encode_frame,
  240. .close = xavs2_close,
  241. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  242. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_NONE },
  243. .priv_class = &libxavs2,
  244. .defaults = xavs2_defaults,
  245. .wrapper_name = "libxavs2",
  246. } ;