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.

453 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. #undef NDEBUG
  29. #include <assert.h>
  30. #include <schroedinger/schro.h>
  31. #include <schroedinger/schrodebug.h>
  32. #include <schroedinger/schrovideoformat.h>
  33. #include "libavutil/attributes.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 being encoded */
  45. AVFrame picture;
  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. } 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 = avpicture_get_size(avctx->pix_fmt,
  131. avctx->width,
  132. avctx->height);
  133. avctx->coded_frame = &p_schro_params->picture;
  134. if (!avctx->gop_size) {
  135. schro_encoder_setting_set_double(p_schro_params->encoder,
  136. "gop_structure",
  137. SCHRO_ENCODER_GOP_INTRA_ONLY);
  138. if (avctx->coder_type == FF_CODER_TYPE_VLC)
  139. schro_encoder_setting_set_double(p_schro_params->encoder,
  140. "enable_noarith", 1);
  141. } else {
  142. schro_encoder_setting_set_double(p_schro_params->encoder,
  143. "au_distance", avctx->gop_size);
  144. avctx->has_b_frames = 1;
  145. p_schro_params->dts = -1;
  146. }
  147. /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
  148. if (avctx->flags & CODEC_FLAG_QSCALE) {
  149. if (!avctx->global_quality) {
  150. /* lossless coding */
  151. schro_encoder_setting_set_double(p_schro_params->encoder,
  152. "rate_control",
  153. SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
  154. } else {
  155. int quality;
  156. schro_encoder_setting_set_double(p_schro_params->encoder,
  157. "rate_control",
  158. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY);
  159. quality = avctx->global_quality / FF_QP2LAMBDA;
  160. if (quality > 10)
  161. quality = 10;
  162. schro_encoder_setting_set_double(p_schro_params->encoder,
  163. "quality", quality);
  164. }
  165. } else {
  166. schro_encoder_setting_set_double(p_schro_params->encoder,
  167. "rate_control",
  168. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
  169. schro_encoder_setting_set_double(p_schro_params->encoder,
  170. "bitrate", avctx->bit_rate);
  171. }
  172. if (avctx->flags & CODEC_FLAG_INTERLACED_ME)
  173. /* All material can be coded as interlaced or progressive
  174. irrespective of the type of source material. */
  175. schro_encoder_setting_set_double(p_schro_params->encoder,
  176. "interlaced_coding", 1);
  177. schro_encoder_setting_set_double(p_schro_params->encoder, "open_gop",
  178. !(avctx->flags & CODEC_FLAG_CLOSED_GOP));
  179. /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
  180. * and libdirac support other bit-depth data. */
  181. schro_video_format_set_std_signal_range(p_schro_params->format,
  182. SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
  183. /* Set the encoder format. */
  184. schro_encoder_set_video_format(p_schro_params->encoder,
  185. p_schro_params->format);
  186. /* Set the debug level. */
  187. schro_debug_set_level(avctx->debug);
  188. schro_encoder_start(p_schro_params->encoder);
  189. /* Initialize the encoded frame queue. */
  190. ff_schro_queue_init(&p_schro_params->enc_frame_queue);
  191. return 0;
  192. }
  193. static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avctx,
  194. const AVFrame *frame)
  195. {
  196. SchroEncoderParams *p_schro_params = avctx->priv_data;
  197. SchroFrame *in_frame;
  198. /* Input line size may differ from what the codec supports. Especially
  199. * when transcoding from one format to another. So use avpicture_layout
  200. * to copy the frame. */
  201. in_frame = ff_create_schro_frame(avctx, p_schro_params->frame_format);
  202. if (in_frame)
  203. avpicture_layout((const AVPicture *)frame, avctx->pix_fmt,
  204. avctx->width, avctx->height,
  205. in_frame->components[0].data,
  206. p_schro_params->frame_size);
  207. return in_frame;
  208. }
  209. static void libschroedinger_free_frame(void *data)
  210. {
  211. FFSchroEncodedFrame *enc_frame = data;
  212. av_freep(&enc_frame->p_encbuf);
  213. av_free(enc_frame);
  214. }
  215. static int libschroedinger_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  216. const AVFrame *frame, int *got_packet)
  217. {
  218. int enc_size = 0;
  219. SchroEncoderParams *p_schro_params = avctx->priv_data;
  220. SchroEncoder *encoder = p_schro_params->encoder;
  221. struct FFSchroEncodedFrame *p_frame_output = NULL;
  222. int go = 1;
  223. SchroBuffer *enc_buf;
  224. int presentation_frame;
  225. int parse_code;
  226. int last_frame_in_sequence = 0;
  227. int pkt_size, ret;
  228. if (!frame) {
  229. /* Push end of sequence if not already signalled. */
  230. if (!p_schro_params->eos_signalled) {
  231. schro_encoder_end_of_stream(encoder);
  232. p_schro_params->eos_signalled = 1;
  233. }
  234. } else {
  235. /* Allocate frame data to schro input buffer. */
  236. SchroFrame *in_frame = libschroedinger_frame_from_data(avctx, frame);
  237. /* Load next frame. */
  238. schro_encoder_push_frame(encoder, in_frame);
  239. }
  240. if (p_schro_params->eos_pulled)
  241. go = 0;
  242. /* Now check to see if we have any output from the encoder. */
  243. while (go) {
  244. SchroStateEnum state;
  245. state = schro_encoder_wait(encoder);
  246. switch (state) {
  247. case SCHRO_STATE_HAVE_BUFFER:
  248. case SCHRO_STATE_END_OF_STREAM:
  249. enc_buf = schro_encoder_pull(encoder, &presentation_frame);
  250. assert(enc_buf->length > 0);
  251. assert(enc_buf->length <= buf_size);
  252. parse_code = enc_buf->data[4];
  253. /* All non-frame data is prepended to actual frame data to
  254. * be able to set the pts correctly. So we don't write data
  255. * to the frame output queue until we actually have a frame
  256. */
  257. p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf,
  258. p_schro_params->enc_buf_size + enc_buf->length);
  259. memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
  260. enc_buf->data, enc_buf->length);
  261. p_schro_params->enc_buf_size += enc_buf->length;
  262. if (state == SCHRO_STATE_END_OF_STREAM) {
  263. p_schro_params->eos_pulled = 1;
  264. go = 0;
  265. }
  266. if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
  267. schro_buffer_unref(enc_buf);
  268. break;
  269. }
  270. /* Create output frame. */
  271. p_frame_output = av_mallocz(sizeof(FFSchroEncodedFrame));
  272. /* Set output data. */
  273. p_frame_output->size = p_schro_params->enc_buf_size;
  274. p_frame_output->p_encbuf = p_schro_params->enc_buf;
  275. if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
  276. SCHRO_PARSE_CODE_IS_REFERENCE(parse_code))
  277. p_frame_output->key_frame = 1;
  278. /* Parse the coded frame number from the bitstream. Bytes 14
  279. * through 17 represesent the frame number. */
  280. p_frame_output->frame_num = AV_RB32(enc_buf->data + 13);
  281. ff_schro_queue_push_back(&p_schro_params->enc_frame_queue,
  282. p_frame_output);
  283. p_schro_params->enc_buf_size = 0;
  284. p_schro_params->enc_buf = NULL;
  285. schro_buffer_unref(enc_buf);
  286. break;
  287. case SCHRO_STATE_NEED_FRAME:
  288. go = 0;
  289. break;
  290. case SCHRO_STATE_AGAIN:
  291. break;
  292. default:
  293. av_log(avctx, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
  294. return -1;
  295. }
  296. }
  297. /* Copy 'next' frame in queue. */
  298. if (p_schro_params->enc_frame_queue.size == 1 &&
  299. p_schro_params->eos_pulled)
  300. last_frame_in_sequence = 1;
  301. p_frame_output = ff_schro_queue_pop(&p_schro_params->enc_frame_queue);
  302. if (!p_frame_output)
  303. return 0;
  304. pkt_size = p_frame_output->size;
  305. if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0)
  306. pkt_size += p_schro_params->enc_buf_size;
  307. if ((ret = ff_alloc_packet(pkt, pkt_size)) < 0) {
  308. av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", pkt_size);
  309. goto error;
  310. }
  311. memcpy(pkt->data, p_frame_output->p_encbuf, p_frame_output->size);
  312. avctx->coded_frame->key_frame = p_frame_output->key_frame;
  313. /* Use the frame number of the encoded frame as the pts. It is OK to
  314. * do so since Dirac is a constant frame rate codec. It expects input
  315. * to be of constant frame rate. */
  316. pkt->pts =
  317. avctx->coded_frame->pts = p_frame_output->frame_num;
  318. pkt->dts = p_schro_params->dts++;
  319. enc_size = p_frame_output->size;
  320. /* Append the end of sequence information to the last frame in the
  321. * sequence. */
  322. if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) {
  323. memcpy(pkt->data + enc_size, p_schro_params->enc_buf,
  324. p_schro_params->enc_buf_size);
  325. enc_size += p_schro_params->enc_buf_size;
  326. av_freep(&p_schro_params->enc_buf);
  327. p_schro_params->enc_buf_size = 0;
  328. }
  329. if (p_frame_output->key_frame)
  330. pkt->flags |= AV_PKT_FLAG_KEY;
  331. *got_packet = 1;
  332. error:
  333. /* free frame */
  334. libschroedinger_free_frame(p_frame_output);
  335. return ret;
  336. }
  337. static int libschroedinger_encode_close(AVCodecContext *avctx)
  338. {
  339. SchroEncoderParams *p_schro_params = avctx->priv_data;
  340. /* Close the encoder. */
  341. schro_encoder_free(p_schro_params->encoder);
  342. /* Free data in the output frame queue. */
  343. ff_schro_queue_free(&p_schro_params->enc_frame_queue,
  344. libschroedinger_free_frame);
  345. /* Free the encoder buffer. */
  346. if (p_schro_params->enc_buf_size)
  347. av_freep(&p_schro_params->enc_buf);
  348. /* Free the video format structure. */
  349. av_freep(&p_schro_params->format);
  350. return 0;
  351. }
  352. AVCodec ff_libschroedinger_encoder = {
  353. .name = "libschroedinger",
  354. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  355. .type = AVMEDIA_TYPE_VIDEO,
  356. .id = AV_CODEC_ID_DIRAC,
  357. .priv_data_size = sizeof(SchroEncoderParams),
  358. .init = libschroedinger_encode_init,
  359. .encode2 = libschroedinger_encode_frame,
  360. .close = libschroedinger_encode_close,
  361. .capabilities = CODEC_CAP_DELAY,
  362. .pix_fmts = (const enum AVPixelFormat[]){
  363. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_NONE
  364. },
  365. };