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.

485 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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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/imgutils.h"
  33. #include "libavutil/opt.h"
  34. #include "avcodec.h"
  35. #include "internal.h"
  36. #include "libschroedinger.h"
  37. #include "bytestream.h"
  38. /** libschroedinger encoder private data */
  39. typedef struct SchroEncoderParams {
  40. /** Schroedinger video format */
  41. SchroVideoFormat *format;
  42. /** Schroedinger frame format */
  43. SchroFrameFormat frame_format;
  44. /** frame size */
  45. int frame_size;
  46. /** Schroedinger encoder handle*/
  47. SchroEncoder* encoder;
  48. /** buffer to store encoder output before writing it to the frame queue*/
  49. unsigned char *enc_buf;
  50. /** Size of encoder buffer*/
  51. int enc_buf_size;
  52. /** queue storing encoded frames */
  53. FFSchroQueue enc_frame_queue;
  54. /** end of sequence signalled */
  55. int eos_signalled;
  56. /** end of sequence pulled */
  57. int eos_pulled;
  58. /* counter for frames submitted to encoder, used as dts */
  59. int64_t dts;
  60. /** enable noarith */
  61. int noarith;
  62. } SchroEncoderParams;
  63. /**
  64. * Works out Schro-compatible chroma format.
  65. */
  66. static int set_chroma_format(AVCodecContext *avctx)
  67. {
  68. int num_formats = sizeof(schro_pixel_format_map) /
  69. sizeof(schro_pixel_format_map[0]);
  70. int idx;
  71. SchroEncoderParams *p_schro_params = avctx->priv_data;
  72. for (idx = 0; idx < num_formats; ++idx) {
  73. if (schro_pixel_format_map[idx].ff_pix_fmt == avctx->pix_fmt) {
  74. p_schro_params->format->chroma_format =
  75. schro_pixel_format_map[idx].schro_pix_fmt;
  76. return 0;
  77. }
  78. }
  79. av_log(avctx, AV_LOG_ERROR,
  80. "This codec currently only supports planar YUV 4:2:0, 4:2:2"
  81. " and 4:4:4 formats.\n");
  82. return -1;
  83. }
  84. static av_cold int libschroedinger_encode_init(AVCodecContext *avctx)
  85. {
  86. SchroEncoderParams *p_schro_params = avctx->priv_data;
  87. SchroVideoFormatEnum preset;
  88. /* Initialize the libraries that libschroedinger depends on. */
  89. schro_init();
  90. /* Create an encoder object. */
  91. p_schro_params->encoder = schro_encoder_new();
  92. if (!p_schro_params->encoder) {
  93. av_log(avctx, AV_LOG_ERROR,
  94. "Unrecoverable Error: schro_encoder_new failed. ");
  95. return -1;
  96. }
  97. /* Initialize the format. */
  98. preset = ff_get_schro_video_format_preset(avctx);
  99. p_schro_params->format =
  100. schro_encoder_get_video_format(p_schro_params->encoder);
  101. schro_video_format_set_std_video_format(p_schro_params->format, preset);
  102. p_schro_params->format->width = avctx->width;
  103. p_schro_params->format->height = avctx->height;
  104. if (set_chroma_format(avctx) == -1)
  105. return -1;
  106. if (avctx->color_primaries == AVCOL_PRI_BT709) {
  107. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_HDTV;
  108. } else if (avctx->color_primaries == AVCOL_PRI_BT470BG) {
  109. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_625;
  110. } else if (avctx->color_primaries == AVCOL_PRI_SMPTE170M) {
  111. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_525;
  112. }
  113. if (avctx->colorspace == AVCOL_SPC_BT709) {
  114. p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_HDTV;
  115. } else if (avctx->colorspace == AVCOL_SPC_BT470BG) {
  116. p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_SDTV;
  117. }
  118. if (avctx->color_trc == AVCOL_TRC_BT709) {
  119. p_schro_params->format->transfer_function = SCHRO_TRANSFER_CHAR_TV_GAMMA;
  120. }
  121. if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
  122. &p_schro_params->frame_format) == -1) {
  123. av_log(avctx, AV_LOG_ERROR,
  124. "This codec currently supports only planar YUV 4:2:0, 4:2:2"
  125. " and 4:4:4 formats.\n");
  126. return -1;
  127. }
  128. p_schro_params->format->frame_rate_numerator = avctx->time_base.den;
  129. p_schro_params->format->frame_rate_denominator = avctx->time_base.num;
  130. p_schro_params->frame_size = av_image_get_buffer_size(avctx->pix_fmt,
  131. avctx->width,
  132. avctx->height, 1);
  133. if (!avctx->gop_size) {
  134. schro_encoder_setting_set_double(p_schro_params->encoder,
  135. "gop_structure",
  136. SCHRO_ENCODER_GOP_INTRA_ONLY);
  137. #if FF_API_CODER_TYPE
  138. FF_DISABLE_DEPRECATION_WARNINGS
  139. if (avctx->coder_type != FF_CODER_TYPE_VLC)
  140. p_schro_params->noarith = 0;
  141. FF_ENABLE_DEPRECATION_WARNINGS
  142. #endif
  143. schro_encoder_setting_set_double(p_schro_params->encoder,
  144. "enable_noarith",
  145. p_schro_params->noarith);
  146. } else {
  147. schro_encoder_setting_set_double(p_schro_params->encoder,
  148. "au_distance", avctx->gop_size);
  149. avctx->has_b_frames = 1;
  150. p_schro_params->dts = -1;
  151. }
  152. /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
  153. if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
  154. if (!avctx->global_quality) {
  155. /* lossless coding */
  156. schro_encoder_setting_set_double(p_schro_params->encoder,
  157. "rate_control",
  158. SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
  159. } else {
  160. int quality;
  161. schro_encoder_setting_set_double(p_schro_params->encoder,
  162. "rate_control",
  163. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY);
  164. quality = avctx->global_quality / FF_QP2LAMBDA;
  165. if (quality > 10)
  166. quality = 10;
  167. schro_encoder_setting_set_double(p_schro_params->encoder,
  168. "quality", quality);
  169. }
  170. } else {
  171. schro_encoder_setting_set_double(p_schro_params->encoder,
  172. "rate_control",
  173. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
  174. schro_encoder_setting_set_double(p_schro_params->encoder,
  175. "bitrate", avctx->bit_rate);
  176. }
  177. if (avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
  178. /* All material can be coded as interlaced or progressive
  179. irrespective of the type of source material. */
  180. schro_encoder_setting_set_double(p_schro_params->encoder,
  181. "interlaced_coding", 1);
  182. schro_encoder_setting_set_double(p_schro_params->encoder, "open_gop",
  183. !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
  184. /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
  185. * and libdirac support other bit-depth data. */
  186. schro_video_format_set_std_signal_range(p_schro_params->format,
  187. SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
  188. /* Set the encoder format. */
  189. schro_encoder_set_video_format(p_schro_params->encoder,
  190. p_schro_params->format);
  191. /* Set the debug level. */
  192. schro_debug_set_level(avctx->debug);
  193. schro_encoder_start(p_schro_params->encoder);
  194. /* Initialize the encoded frame queue. */
  195. ff_schro_queue_init(&p_schro_params->enc_frame_queue);
  196. return 0;
  197. }
  198. static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avctx,
  199. const AVFrame *frame)
  200. {
  201. SchroEncoderParams *p_schro_params = avctx->priv_data;
  202. SchroFrame *in_frame = ff_create_schro_frame(avctx,
  203. p_schro_params->frame_format);
  204. if (in_frame) {
  205. /* Copy input data to SchroFrame buffers (they match the ones
  206. * referenced by the AVFrame stored in priv) */
  207. if (av_frame_copy(in_frame->priv, frame) < 0) {
  208. av_log(avctx, AV_LOG_ERROR, "Failed to copy input data\n");
  209. return NULL;
  210. }
  211. }
  212. return in_frame;
  213. }
  214. static void libschroedinger_free_frame(void *data)
  215. {
  216. FFSchroEncodedFrame *enc_frame = data;
  217. av_freep(&enc_frame->p_encbuf);
  218. av_free(enc_frame);
  219. }
  220. static int libschroedinger_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  221. const AVFrame *frame, int *got_packet)
  222. {
  223. int enc_size = 0;
  224. SchroEncoderParams *p_schro_params = avctx->priv_data;
  225. SchroEncoder *encoder = p_schro_params->encoder;
  226. struct FFSchroEncodedFrame *p_frame_output = NULL;
  227. int go = 1;
  228. SchroBuffer *enc_buf;
  229. int presentation_frame;
  230. int parse_code;
  231. int last_frame_in_sequence = 0;
  232. int pkt_size, ret;
  233. if (!frame) {
  234. /* Push end of sequence if not already signalled. */
  235. if (!p_schro_params->eos_signalled) {
  236. schro_encoder_end_of_stream(encoder);
  237. p_schro_params->eos_signalled = 1;
  238. }
  239. } else {
  240. /* Allocate frame data to schro input buffer. */
  241. SchroFrame *in_frame = libschroedinger_frame_from_data(avctx, frame);
  242. if (!in_frame)
  243. return AVERROR(ENOMEM);
  244. /* Load next frame. */
  245. schro_encoder_push_frame(encoder, in_frame);
  246. }
  247. if (p_schro_params->eos_pulled)
  248. go = 0;
  249. /* Now check to see if we have any output from the encoder. */
  250. while (go) {
  251. int err;
  252. SchroStateEnum state;
  253. state = schro_encoder_wait(encoder);
  254. switch (state) {
  255. case SCHRO_STATE_HAVE_BUFFER:
  256. case SCHRO_STATE_END_OF_STREAM:
  257. enc_buf = schro_encoder_pull(encoder, &presentation_frame);
  258. if (enc_buf->length <= 0)
  259. return AVERROR_BUG;
  260. parse_code = enc_buf->data[4];
  261. /* All non-frame data is prepended to actual frame data to
  262. * be able to set the pts correctly. So we don't write data
  263. * to the frame output queue until we actually have a frame
  264. */
  265. if ((err = av_reallocp(&p_schro_params->enc_buf,
  266. p_schro_params->enc_buf_size +
  267. enc_buf->length)) < 0) {
  268. p_schro_params->enc_buf_size = 0;
  269. return err;
  270. }
  271. memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
  272. enc_buf->data, enc_buf->length);
  273. p_schro_params->enc_buf_size += enc_buf->length;
  274. if (state == SCHRO_STATE_END_OF_STREAM) {
  275. p_schro_params->eos_pulled = 1;
  276. go = 0;
  277. }
  278. if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
  279. schro_buffer_unref(enc_buf);
  280. break;
  281. }
  282. /* Create output frame. */
  283. p_frame_output = av_mallocz(sizeof(FFSchroEncodedFrame));
  284. if (!p_frame_output)
  285. return AVERROR(ENOMEM);
  286. /* Set output data. */
  287. p_frame_output->size = p_schro_params->enc_buf_size;
  288. p_frame_output->p_encbuf = p_schro_params->enc_buf;
  289. if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
  290. SCHRO_PARSE_CODE_IS_REFERENCE(parse_code))
  291. p_frame_output->key_frame = 1;
  292. /* Parse the coded frame number from the bitstream. Bytes 14
  293. * through 17 represent the frame number. */
  294. p_frame_output->frame_num = AV_RB32(enc_buf->data + 13);
  295. ff_schro_queue_push_back(&p_schro_params->enc_frame_queue,
  296. p_frame_output);
  297. p_schro_params->enc_buf_size = 0;
  298. p_schro_params->enc_buf = NULL;
  299. schro_buffer_unref(enc_buf);
  300. break;
  301. case SCHRO_STATE_NEED_FRAME:
  302. go = 0;
  303. break;
  304. case SCHRO_STATE_AGAIN:
  305. break;
  306. default:
  307. av_log(avctx, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
  308. return -1;
  309. }
  310. }
  311. /* Copy 'next' frame in queue. */
  312. if (p_schro_params->enc_frame_queue.size == 1 &&
  313. p_schro_params->eos_pulled)
  314. last_frame_in_sequence = 1;
  315. p_frame_output = ff_schro_queue_pop(&p_schro_params->enc_frame_queue);
  316. if (!p_frame_output)
  317. return 0;
  318. pkt_size = p_frame_output->size;
  319. if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0)
  320. pkt_size += p_schro_params->enc_buf_size;
  321. if ((ret = ff_alloc_packet(pkt, pkt_size)) < 0) {
  322. av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", pkt_size);
  323. goto error;
  324. }
  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. };