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.

486 lines
17KB

  1. /*
  2. * Dirac encoder support via Schroedinger libraries
  3. * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Dirac encoder support via libschroedinger-1.0 libraries. More details about
  24. * the Schroedinger project can be found at http://www.diracvideo.org/.
  25. * The library implements Dirac Specification Version 2.2
  26. * (http://dirac.sourceforge.net/specification.html).
  27. */
  28. #include <schroedinger/schro.h>
  29. #include <schroedinger/schrodebug.h>
  30. #include <schroedinger/schrovideoformat.h>
  31. #include "libavutil/attributes.h"
  32. #include "libavutil/avassert.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/opt.h"
  35. #include "avcodec.h"
  36. #include "internal.h"
  37. #include "libschroedinger.h"
  38. #include "bytestream.h"
  39. /** libschroedinger encoder private data */
  40. typedef struct SchroEncoderParams {
  41. AVClass *class;
  42. /** Schroedinger video format */
  43. SchroVideoFormat *format;
  44. /** Schroedinger frame format */
  45. SchroFrameFormat frame_format;
  46. /** frame size */
  47. int frame_size;
  48. /** Schroedinger encoder handle*/
  49. SchroEncoder* encoder;
  50. /** buffer to store encoder output before writing it to the frame queue*/
  51. unsigned char *enc_buf;
  52. /** Size of encoder buffer*/
  53. int enc_buf_size;
  54. /** queue storing encoded frames */
  55. FFSchroQueue enc_frame_queue;
  56. /** end of sequence signalled */
  57. int eos_signalled;
  58. /** end of sequence pulled */
  59. int eos_pulled;
  60. /* counter for frames submitted to encoder, used as dts */
  61. int64_t dts;
  62. /** enable noarith */
  63. int noarith;
  64. } SchroEncoderParams;
  65. /**
  66. * Works out Schro-compatible chroma format.
  67. */
  68. static int set_chroma_format(AVCodecContext *avctx)
  69. {
  70. int num_formats = sizeof(schro_pixel_format_map) /
  71. sizeof(schro_pixel_format_map[0]);
  72. int idx;
  73. SchroEncoderParams *p_schro_params = avctx->priv_data;
  74. for (idx = 0; idx < num_formats; ++idx) {
  75. if (schro_pixel_format_map[idx].ff_pix_fmt == avctx->pix_fmt) {
  76. p_schro_params->format->chroma_format =
  77. schro_pixel_format_map[idx].schro_pix_fmt;
  78. return 0;
  79. }
  80. }
  81. av_log(avctx, AV_LOG_ERROR,
  82. "This codec currently only supports planar YUV 4:2:0, 4:2:2"
  83. " and 4:4:4 formats.\n");
  84. return -1;
  85. }
  86. static av_cold int libschroedinger_encode_init(AVCodecContext *avctx)
  87. {
  88. SchroEncoderParams *p_schro_params = avctx->priv_data;
  89. SchroVideoFormatEnum preset;
  90. /* Initialize the libraries that libschroedinger depends on. */
  91. schro_init();
  92. /* Create an encoder object. */
  93. p_schro_params->encoder = schro_encoder_new();
  94. if (!p_schro_params->encoder) {
  95. av_log(avctx, AV_LOG_ERROR,
  96. "Unrecoverable Error: schro_encoder_new failed. ");
  97. return -1;
  98. }
  99. /* Initialize the format. */
  100. preset = ff_get_schro_video_format_preset(avctx);
  101. p_schro_params->format =
  102. schro_encoder_get_video_format(p_schro_params->encoder);
  103. schro_video_format_set_std_video_format(p_schro_params->format, preset);
  104. p_schro_params->format->width = avctx->width;
  105. p_schro_params->format->height = avctx->height;
  106. if (set_chroma_format(avctx) == -1)
  107. return -1;
  108. if (avctx->color_primaries == AVCOL_PRI_BT709) {
  109. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_HDTV;
  110. } else if (avctx->color_primaries == AVCOL_PRI_BT470BG) {
  111. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_625;
  112. } else if (avctx->color_primaries == AVCOL_PRI_SMPTE170M) {
  113. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_525;
  114. }
  115. if (avctx->colorspace == AVCOL_SPC_BT709) {
  116. p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_HDTV;
  117. } else if (avctx->colorspace == AVCOL_SPC_BT470BG) {
  118. p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_SDTV;
  119. }
  120. if (avctx->color_trc == AVCOL_TRC_BT709) {
  121. p_schro_params->format->transfer_function = SCHRO_TRANSFER_CHAR_TV_GAMMA;
  122. }
  123. if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
  124. &p_schro_params->frame_format) == -1) {
  125. av_log(avctx, AV_LOG_ERROR,
  126. "This codec currently supports only planar YUV 4:2:0, 4:2:2"
  127. " and 4:4:4 formats.\n");
  128. return -1;
  129. }
  130. p_schro_params->format->frame_rate_numerator = avctx->time_base.den;
  131. p_schro_params->format->frame_rate_denominator = avctx->time_base.num;
  132. p_schro_params->frame_size = av_image_get_buffer_size(avctx->pix_fmt,
  133. avctx->width,
  134. avctx->height, 1);
  135. if (!avctx->gop_size) {
  136. schro_encoder_setting_set_double(p_schro_params->encoder,
  137. "gop_structure",
  138. SCHRO_ENCODER_GOP_INTRA_ONLY);
  139. #if FF_API_CODER_TYPE
  140. FF_DISABLE_DEPRECATION_WARNINGS
  141. if (avctx->coder_type != FF_CODER_TYPE_VLC)
  142. p_schro_params->noarith = 0;
  143. FF_ENABLE_DEPRECATION_WARNINGS
  144. #endif
  145. schro_encoder_setting_set_double(p_schro_params->encoder,
  146. "enable_noarith",
  147. p_schro_params->noarith);
  148. } else {
  149. schro_encoder_setting_set_double(p_schro_params->encoder,
  150. "au_distance", avctx->gop_size);
  151. avctx->has_b_frames = 1;
  152. p_schro_params->dts = -1;
  153. }
  154. /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
  155. if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
  156. if (!avctx->global_quality) {
  157. /* lossless coding */
  158. schro_encoder_setting_set_double(p_schro_params->encoder,
  159. "rate_control",
  160. SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
  161. } else {
  162. int quality;
  163. schro_encoder_setting_set_double(p_schro_params->encoder,
  164. "rate_control",
  165. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY);
  166. quality = avctx->global_quality / FF_QP2LAMBDA;
  167. if (quality > 10)
  168. quality = 10;
  169. schro_encoder_setting_set_double(p_schro_params->encoder,
  170. "quality", quality);
  171. }
  172. } else {
  173. schro_encoder_setting_set_double(p_schro_params->encoder,
  174. "rate_control",
  175. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
  176. schro_encoder_setting_set_double(p_schro_params->encoder,
  177. "bitrate", avctx->bit_rate);
  178. }
  179. if (avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
  180. /* All material can be coded as interlaced or progressive
  181. irrespective of the type of source material. */
  182. schro_encoder_setting_set_double(p_schro_params->encoder,
  183. "interlaced_coding", 1);
  184. schro_encoder_setting_set_double(p_schro_params->encoder, "open_gop",
  185. !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
  186. /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
  187. * and libdirac support other bit-depth data. */
  188. schro_video_format_set_std_signal_range(p_schro_params->format,
  189. SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
  190. /* Set the encoder format. */
  191. schro_encoder_set_video_format(p_schro_params->encoder,
  192. p_schro_params->format);
  193. /* Set the debug level. */
  194. schro_debug_set_level(avctx->debug);
  195. schro_encoder_start(p_schro_params->encoder);
  196. /* Initialize the encoded frame queue. */
  197. ff_schro_queue_init(&p_schro_params->enc_frame_queue);
  198. return 0;
  199. }
  200. static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avctx,
  201. const AVFrame *frame)
  202. {
  203. SchroEncoderParams *p_schro_params = avctx->priv_data;
  204. SchroFrame *in_frame = ff_create_schro_frame(avctx,
  205. p_schro_params->frame_format);
  206. if (in_frame) {
  207. /* Copy input data to SchroFrame buffers (they match the ones
  208. * referenced by the AVFrame stored in priv) */
  209. if (av_frame_copy(in_frame->priv, frame) < 0) {
  210. av_log(avctx, AV_LOG_ERROR, "Failed to copy input data\n");
  211. return NULL;
  212. }
  213. }
  214. return in_frame;
  215. }
  216. static void libschroedinger_free_frame(void *data)
  217. {
  218. FFSchroEncodedFrame *enc_frame = data;
  219. av_freep(&enc_frame->p_encbuf);
  220. av_free(enc_frame);
  221. }
  222. static int libschroedinger_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  223. const AVFrame *frame, int *got_packet)
  224. {
  225. int enc_size = 0;
  226. SchroEncoderParams *p_schro_params = avctx->priv_data;
  227. SchroEncoder *encoder = p_schro_params->encoder;
  228. struct FFSchroEncodedFrame *p_frame_output = NULL;
  229. int go = 1;
  230. SchroBuffer *enc_buf;
  231. int presentation_frame;
  232. int parse_code;
  233. int last_frame_in_sequence = 0;
  234. int pkt_size, ret;
  235. if (!frame) {
  236. /* Push end of sequence if not already signalled. */
  237. if (!p_schro_params->eos_signalled) {
  238. schro_encoder_end_of_stream(encoder);
  239. p_schro_params->eos_signalled = 1;
  240. }
  241. } else {
  242. /* Allocate frame data to schro input buffer. */
  243. SchroFrame *in_frame = libschroedinger_frame_from_data(avctx, frame);
  244. if (!in_frame)
  245. return AVERROR(ENOMEM);
  246. /* Load next frame. */
  247. schro_encoder_push_frame(encoder, in_frame);
  248. }
  249. if (p_schro_params->eos_pulled)
  250. go = 0;
  251. /* Now check to see if we have any output from the encoder. */
  252. while (go) {
  253. int err;
  254. SchroStateEnum state;
  255. state = schro_encoder_wait(encoder);
  256. switch (state) {
  257. case SCHRO_STATE_HAVE_BUFFER:
  258. case SCHRO_STATE_END_OF_STREAM:
  259. enc_buf = schro_encoder_pull(encoder, &presentation_frame);
  260. if (enc_buf->length <= 0)
  261. return AVERROR_BUG;
  262. parse_code = enc_buf->data[4];
  263. /* All non-frame data is prepended to actual frame data to
  264. * be able to set the pts correctly. So we don't write data
  265. * to the frame output queue until we actually have a frame
  266. */
  267. if ((err = av_reallocp(&p_schro_params->enc_buf,
  268. p_schro_params->enc_buf_size +
  269. enc_buf->length)) < 0) {
  270. p_schro_params->enc_buf_size = 0;
  271. return err;
  272. }
  273. memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
  274. enc_buf->data, enc_buf->length);
  275. p_schro_params->enc_buf_size += enc_buf->length;
  276. if (state == SCHRO_STATE_END_OF_STREAM) {
  277. p_schro_params->eos_pulled = 1;
  278. go = 0;
  279. }
  280. if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
  281. schro_buffer_unref(enc_buf);
  282. break;
  283. }
  284. /* Create output frame. */
  285. p_frame_output = av_mallocz(sizeof(FFSchroEncodedFrame));
  286. if (!p_frame_output)
  287. return AVERROR(ENOMEM);
  288. /* Set output data. */
  289. p_frame_output->size = p_schro_params->enc_buf_size;
  290. p_frame_output->p_encbuf = p_schro_params->enc_buf;
  291. if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
  292. SCHRO_PARSE_CODE_IS_REFERENCE(parse_code))
  293. p_frame_output->key_frame = 1;
  294. /* Parse the coded frame number from the bitstream. Bytes 14
  295. * through 17 represent the frame number. */
  296. p_frame_output->frame_num = AV_RB32(enc_buf->data + 13);
  297. ff_schro_queue_push_back(&p_schro_params->enc_frame_queue,
  298. p_frame_output);
  299. p_schro_params->enc_buf_size = 0;
  300. p_schro_params->enc_buf = NULL;
  301. schro_buffer_unref(enc_buf);
  302. break;
  303. case SCHRO_STATE_NEED_FRAME:
  304. go = 0;
  305. break;
  306. case SCHRO_STATE_AGAIN:
  307. break;
  308. default:
  309. av_log(avctx, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
  310. return -1;
  311. }
  312. }
  313. /* Copy 'next' frame in queue. */
  314. if (p_schro_params->enc_frame_queue.size == 1 &&
  315. p_schro_params->eos_pulled)
  316. last_frame_in_sequence = 1;
  317. p_frame_output = ff_schro_queue_pop(&p_schro_params->enc_frame_queue);
  318. if (!p_frame_output)
  319. return 0;
  320. pkt_size = p_frame_output->size;
  321. if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0)
  322. pkt_size += p_schro_params->enc_buf_size;
  323. if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
  324. goto error;
  325. memcpy(pkt->data, p_frame_output->p_encbuf, p_frame_output->size);
  326. #if FF_API_CODED_FRAME
  327. FF_DISABLE_DEPRECATION_WARNINGS
  328. avctx->coded_frame->key_frame = p_frame_output->key_frame;
  329. avctx->coded_frame->pts = p_frame_output->frame_num;
  330. FF_ENABLE_DEPRECATION_WARNINGS
  331. #endif
  332. /* Use the frame number of the encoded frame as the pts. It is OK to
  333. * do so since Dirac is a constant frame rate codec. It expects input
  334. * to be of constant frame rate. */
  335. pkt->pts = p_frame_output->frame_num;
  336. pkt->dts = p_schro_params->dts++;
  337. enc_size = p_frame_output->size;
  338. /* Append the end of sequence information to the last frame in the
  339. * sequence. */
  340. if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) {
  341. memcpy(pkt->data + enc_size, p_schro_params->enc_buf,
  342. p_schro_params->enc_buf_size);
  343. enc_size += p_schro_params->enc_buf_size;
  344. av_freep(&p_schro_params->enc_buf);
  345. p_schro_params->enc_buf_size = 0;
  346. }
  347. if (p_frame_output->key_frame)
  348. pkt->flags |= AV_PKT_FLAG_KEY;
  349. *got_packet = 1;
  350. error:
  351. /* free frame */
  352. libschroedinger_free_frame(p_frame_output);
  353. return ret;
  354. }
  355. static int libschroedinger_encode_close(AVCodecContext *avctx)
  356. {
  357. SchroEncoderParams *p_schro_params = avctx->priv_data;
  358. /* Close the encoder. */
  359. schro_encoder_free(p_schro_params->encoder);
  360. /* Free data in the output frame queue. */
  361. ff_schro_queue_free(&p_schro_params->enc_frame_queue,
  362. libschroedinger_free_frame);
  363. /* Free the encoder buffer. */
  364. if (p_schro_params->enc_buf_size)
  365. av_freep(&p_schro_params->enc_buf);
  366. /* Free the video format structure. */
  367. av_freep(&p_schro_params->format);
  368. return 0;
  369. }
  370. #define OFFSET(x) offsetof(SchroEncoderParams, x)
  371. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  372. static const AVOption options[] = {
  373. { "noarith", "Enable noarith", OFFSET(noarith), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
  374. { NULL },
  375. };
  376. static const AVClass libschroedinger_class = {
  377. .class_name = "libschroedinger",
  378. .item_name = av_default_item_name,
  379. .option = options,
  380. .version = LIBAVUTIL_VERSION_INT,
  381. };
  382. AVCodec ff_libschroedinger_encoder = {
  383. .name = "libschroedinger",
  384. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  385. .type = AVMEDIA_TYPE_VIDEO,
  386. .id = AV_CODEC_ID_DIRAC,
  387. .priv_data_size = sizeof(SchroEncoderParams),
  388. .priv_class = &libschroedinger_class,
  389. .init = libschroedinger_encode_init,
  390. .encode2 = libschroedinger_encode_frame,
  391. .close = libschroedinger_encode_close,
  392. .capabilities = AV_CODEC_CAP_DELAY,
  393. .pix_fmts = (const enum AVPixelFormat[]){
  394. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_NONE
  395. },
  396. };