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.

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