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.

451 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 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/avassert.h"
  32. #include "avcodec.h"
  33. #include "internal.h"
  34. #include "libschroedinger.h"
  35. #include "bytestream.h"
  36. /** libschroedinger encoder private data */
  37. typedef struct SchroEncoderParams {
  38. /** Schroedinger video format */
  39. SchroVideoFormat *format;
  40. /** Schroedinger frame format */
  41. SchroFrameFormat frame_format;
  42. /** frame being encoded */
  43. AVFrame picture;
  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. } SchroEncoderParams;
  61. /**
  62. * Works out Schro-compatible chroma format.
  63. */
  64. static int set_chroma_format(AVCodecContext *avccontext)
  65. {
  66. int num_formats = sizeof(schro_pixel_format_map) /
  67. sizeof(schro_pixel_format_map[0]);
  68. int idx;
  69. SchroEncoderParams *p_schro_params = avccontext->priv_data;
  70. for (idx = 0; idx < num_formats; ++idx) {
  71. if (schro_pixel_format_map[idx].ff_pix_fmt ==
  72. avccontext->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(avccontext, 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 *avccontext)
  84. {
  85. SchroEncoderParams *p_schro_params = avccontext->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(avccontext, 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(avccontext);
  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 = avccontext->width;
  102. p_schro_params->format->height = avccontext->height;
  103. if (set_chroma_format(avccontext) == -1)
  104. return -1;
  105. if (avccontext->color_primaries == AVCOL_PRI_BT709) {
  106. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_HDTV;
  107. } else if (avccontext->color_primaries == AVCOL_PRI_BT470BG) {
  108. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_625;
  109. } else if (avccontext->color_primaries == AVCOL_PRI_SMPTE170M) {
  110. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_525;
  111. }
  112. if (avccontext->colorspace == AVCOL_SPC_BT709) {
  113. p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_HDTV;
  114. } else if (avccontext->colorspace == AVCOL_SPC_BT470BG) {
  115. p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_SDTV;
  116. }
  117. if (avccontext->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(avccontext, 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 = avccontext->time_base.den;
  128. p_schro_params->format->frame_rate_denominator = avccontext->time_base.num;
  129. p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
  130. avccontext->width,
  131. avccontext->height);
  132. avccontext->coded_frame = &p_schro_params->picture;
  133. if (!avccontext->gop_size) {
  134. schro_encoder_setting_set_double(p_schro_params->encoder,
  135. "gop_structure",
  136. SCHRO_ENCODER_GOP_INTRA_ONLY);
  137. if (avccontext->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", avccontext->gop_size);
  143. avccontext->has_b_frames = 1;
  144. p_schro_params->dts = -1;
  145. }
  146. /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
  147. if (avccontext->flags & CODEC_FLAG_QSCALE) {
  148. if (!avccontext->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 = avccontext->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",
  170. avccontext->bit_rate);
  171. }
  172. if (avccontext->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. !(avccontext->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(avccontext->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 *avccontext,
  194. const AVFrame *frame)
  195. {
  196. SchroEncoderParams *p_schro_params = avccontext->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(avccontext, p_schro_params->frame_format);
  202. if (in_frame)
  203. avpicture_layout((const AVPicture *)frame, avccontext->pix_fmt,
  204. avccontext->width, avccontext->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 *avccontext, AVPacket *pkt,
  216. const AVFrame *frame, int *got_packet)
  217. {
  218. int enc_size = 0;
  219. SchroEncoderParams *p_schro_params = avccontext->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(avccontext,
  237. frame);
  238. /* Load next frame. */
  239. schro_encoder_push_frame(encoder, in_frame);
  240. }
  241. if (p_schro_params->eos_pulled)
  242. go = 0;
  243. /* Now check to see if we have any output from the encoder. */
  244. while (go) {
  245. SchroStateEnum state;
  246. state = schro_encoder_wait(encoder);
  247. switch (state) {
  248. case SCHRO_STATE_HAVE_BUFFER:
  249. case SCHRO_STATE_END_OF_STREAM:
  250. enc_buf = schro_encoder_pull(encoder, &presentation_frame);
  251. av_assert0(enc_buf->length > 0);
  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(avccontext, 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_packet2(avccontext, pkt, pkt_size)) < 0)
  308. goto error;
  309. memcpy(pkt->data, p_frame_output->p_encbuf, p_frame_output->size);
  310. avccontext->coded_frame->key_frame = p_frame_output->key_frame;
  311. /* Use the frame number of the encoded frame as the pts. It is OK to
  312. * do so since Dirac is a constant frame rate codec. It expects input
  313. * to be of constant frame rate. */
  314. pkt->pts =
  315. avccontext->coded_frame->pts = p_frame_output->frame_num;
  316. pkt->dts = p_schro_params->dts++;
  317. enc_size = p_frame_output->size;
  318. /* Append the end of sequence information to the last frame in the
  319. * sequence. */
  320. if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) {
  321. memcpy(pkt->data + enc_size, p_schro_params->enc_buf,
  322. p_schro_params->enc_buf_size);
  323. enc_size += p_schro_params->enc_buf_size;
  324. av_freep(&p_schro_params->enc_buf);
  325. p_schro_params->enc_buf_size = 0;
  326. }
  327. if (p_frame_output->key_frame)
  328. pkt->flags |= AV_PKT_FLAG_KEY;
  329. *got_packet = 1;
  330. error:
  331. /* free frame */
  332. libschroedinger_free_frame(p_frame_output);
  333. return ret;
  334. }
  335. static int libschroedinger_encode_close(AVCodecContext *avccontext)
  336. {
  337. SchroEncoderParams *p_schro_params = avccontext->priv_data;
  338. /* Close the encoder. */
  339. schro_encoder_free(p_schro_params->encoder);
  340. /* Free data in the output frame queue. */
  341. ff_schro_queue_free(&p_schro_params->enc_frame_queue,
  342. libschroedinger_free_frame);
  343. /* Free the encoder buffer. */
  344. if (p_schro_params->enc_buf_size)
  345. av_freep(&p_schro_params->enc_buf);
  346. /* Free the video format structure. */
  347. av_freep(&p_schro_params->format);
  348. return 0;
  349. }
  350. AVCodec ff_libschroedinger_encoder = {
  351. .name = "libschroedinger",
  352. .type = AVMEDIA_TYPE_VIDEO,
  353. .id = AV_CODEC_ID_DIRAC,
  354. .priv_data_size = sizeof(SchroEncoderParams),
  355. .init = libschroedinger_encode_init,
  356. .encode2 = libschroedinger_encode_frame,
  357. .close = libschroedinger_encode_close,
  358. .capabilities = CODEC_CAP_DELAY,
  359. .pix_fmts = (const enum PixelFormat[]){
  360. PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE
  361. },
  362. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  363. };