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.

460 lines
16KB

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