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