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.

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