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.

406 lines
14KB

  1. /*
  2. * Dirac encoding support via libdirac library
  3. * Copyright (c) 2005 BBC, Andrew Kennedy <dirac at rd dot bbc dot co dot uk>
  4. * Copyright (c) 2006-2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavcodec/libdiracenc.c
  24. * Dirac encoding support via libdirac library; more details about the
  25. * Dirac project can be found at http://dirac.sourceforge.net/.
  26. * The libdirac_encoder library implements Dirac specification version 2.2
  27. * (http://dirac.sourceforge.net/specification.html).
  28. */
  29. #include "libdirac_libschro.h"
  30. #include "libdirac.h"
  31. #undef NDEBUG
  32. #include <assert.h>
  33. #include <libdirac_encoder/dirac_encoder.h>
  34. /** Dirac encoder private data */
  35. typedef struct FfmpegDiracEncoderParams {
  36. /** Dirac encoder context */
  37. dirac_encoder_context_t enc_ctx;
  38. /** frame being encoded */
  39. AVFrame picture;
  40. /** frame size */
  41. int frame_size;
  42. /** Dirac encoder handle */
  43. dirac_encoder_t* p_encoder;
  44. /** input frame buffer */
  45. unsigned char *p_in_frame_buf;
  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. FfmpegDiracSchroQueue enc_frame_queue;
  52. /** end of sequence signalled by user, 0 - false, 1 - true */
  53. int eos_signalled;
  54. /** end of sequence returned by encoder, 0 - false, 1 - true */
  55. int eos_pulled;
  56. } FfmpegDiracEncoderParams;
  57. /**
  58. * Works out Dirac-compatible chroma format.
  59. */
  60. static dirac_chroma_t GetDiracChromaFormat(enum PixelFormat ff_pix_fmt)
  61. {
  62. int num_formats = sizeof(ffmpeg_dirac_pixel_format_map) /
  63. sizeof(ffmpeg_dirac_pixel_format_map[0]);
  64. int idx;
  65. for (idx = 0; idx < num_formats; ++idx)
  66. if (ffmpeg_dirac_pixel_format_map[idx].ff_pix_fmt == ff_pix_fmt)
  67. return ffmpeg_dirac_pixel_format_map[idx].dirac_pix_fmt;
  68. return formatNK;
  69. }
  70. /**
  71. * Dirac video preset table. Ensure that this tables matches up correctly
  72. * with the ff_dirac_schro_video_format_info table in libdirac_libschro.c.
  73. */
  74. static const VideoFormat ff_dirac_video_formats[]={
  75. VIDEO_FORMAT_CUSTOM ,
  76. VIDEO_FORMAT_QSIF525 ,
  77. VIDEO_FORMAT_QCIF ,
  78. VIDEO_FORMAT_SIF525 ,
  79. VIDEO_FORMAT_CIF ,
  80. VIDEO_FORMAT_4SIF525 ,
  81. VIDEO_FORMAT_4CIF ,
  82. VIDEO_FORMAT_SD_480I60 ,
  83. VIDEO_FORMAT_SD_576I50 ,
  84. VIDEO_FORMAT_HD_720P60 ,
  85. VIDEO_FORMAT_HD_720P50 ,
  86. VIDEO_FORMAT_HD_1080I60 ,
  87. VIDEO_FORMAT_HD_1080I50 ,
  88. VIDEO_FORMAT_HD_1080P60 ,
  89. VIDEO_FORMAT_HD_1080P50 ,
  90. VIDEO_FORMAT_DIGI_CINEMA_2K24 ,
  91. VIDEO_FORMAT_DIGI_CINEMA_4K24 ,
  92. };
  93. /**
  94. * Returns the video format preset matching the input video dimensions and
  95. * time base.
  96. */
  97. static VideoFormat GetDiracVideoFormatPreset(AVCodecContext *avccontext)
  98. {
  99. unsigned int num_formats = sizeof(ff_dirac_video_formats) /
  100. sizeof(ff_dirac_video_formats[0]);
  101. unsigned int idx = ff_dirac_schro_get_video_format_idx(avccontext);
  102. return (idx < num_formats) ?
  103. ff_dirac_video_formats[idx] : VIDEO_FORMAT_CUSTOM;
  104. }
  105. static av_cold int libdirac_encode_init(AVCodecContext *avccontext)
  106. {
  107. FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data;
  108. int no_local = 1;
  109. int verbose = avccontext->debug;
  110. VideoFormat preset;
  111. /* get Dirac preset */
  112. preset = GetDiracVideoFormatPreset(avccontext);
  113. /* initialize the encoder context */
  114. dirac_encoder_context_init(&(p_dirac_params->enc_ctx), preset);
  115. p_dirac_params->enc_ctx.src_params.chroma = GetDiracChromaFormat(avccontext->pix_fmt);
  116. if (p_dirac_params->enc_ctx.src_params.chroma == formatNK) {
  117. av_log(avccontext, AV_LOG_ERROR,
  118. "Unsupported pixel format %d. This codec supports only "
  119. "Planar YUV formats (yuv420p, yuv422p, yuv444p\n",
  120. avccontext->pix_fmt);
  121. return -1;
  122. }
  123. p_dirac_params->enc_ctx.src_params.frame_rate.numerator = avccontext->time_base.den;
  124. p_dirac_params->enc_ctx.src_params.frame_rate.denominator = avccontext->time_base.num;
  125. p_dirac_params->enc_ctx.src_params.width = avccontext->width;
  126. p_dirac_params->enc_ctx.src_params.height = avccontext->height;
  127. p_dirac_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
  128. avccontext->width,
  129. avccontext->height);
  130. avccontext->coded_frame = &p_dirac_params->picture;
  131. if (no_local) {
  132. p_dirac_params->enc_ctx.decode_flag = 0;
  133. p_dirac_params->enc_ctx.instr_flag = 0;
  134. } else {
  135. p_dirac_params->enc_ctx.decode_flag = 1;
  136. p_dirac_params->enc_ctx.instr_flag = 1;
  137. }
  138. /* Intra-only sequence */
  139. if (!avccontext->gop_size) {
  140. p_dirac_params->enc_ctx.enc_params.num_L1 = 0;
  141. if (avccontext->coder_type == FF_CODER_TYPE_VLC)
  142. p_dirac_params->enc_ctx.enc_params.using_ac = 0;
  143. } else
  144. avccontext->has_b_frames = 1;
  145. if (avccontext->flags & CODEC_FLAG_QSCALE) {
  146. if (avccontext->global_quality) {
  147. p_dirac_params->enc_ctx.enc_params.qf = avccontext->global_quality
  148. / (FF_QP2LAMBDA * 10.0);
  149. /* if it is not default bitrate then send target rate. */
  150. if (avccontext->bit_rate >= 1000 &&
  151. avccontext->bit_rate != 200000)
  152. p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate
  153. / 1000;
  154. } else
  155. p_dirac_params->enc_ctx.enc_params.lossless = 1;
  156. } else if (avccontext->bit_rate >= 1000)
  157. p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate / 1000;
  158. if ((preset > VIDEO_FORMAT_QCIF || preset < VIDEO_FORMAT_QSIF525) &&
  159. avccontext->bit_rate == 200000)
  160. p_dirac_params->enc_ctx.enc_params.trate = 0;
  161. if (avccontext->flags & CODEC_FLAG_INTERLACED_ME)
  162. /* all material can be coded as interlaced or progressive
  163. * irrespective of the type of source material */
  164. p_dirac_params->enc_ctx.enc_params.picture_coding_mode = 1;
  165. p_dirac_params->p_encoder = dirac_encoder_init(&(p_dirac_params->enc_ctx),
  166. verbose);
  167. if (!p_dirac_params->p_encoder) {
  168. av_log(avccontext, AV_LOG_ERROR,
  169. "Unrecoverable Error: dirac_encoder_init failed. ");
  170. return EXIT_FAILURE;
  171. }
  172. /* allocate enough memory for the incoming data */
  173. p_dirac_params->p_in_frame_buf = av_malloc(p_dirac_params->frame_size);
  174. /* initialize the encoded frame queue */
  175. ff_dirac_schro_queue_init(&p_dirac_params->enc_frame_queue);
  176. return 0;
  177. }
  178. static void DiracFreeFrame(void *data)
  179. {
  180. FfmpegDiracSchroEncodedFrame *enc_frame = data;
  181. av_freep(&(enc_frame->p_encbuf));
  182. av_free(enc_frame);
  183. }
  184. static int libdirac_encode_frame(AVCodecContext *avccontext,
  185. unsigned char *frame,
  186. int buf_size, void *data)
  187. {
  188. int enc_size = 0;
  189. dirac_encoder_state_t state;
  190. FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data;
  191. FfmpegDiracSchroEncodedFrame* p_frame_output = NULL;
  192. FfmpegDiracSchroEncodedFrame* p_next_output_frame = NULL;
  193. int go = 1;
  194. int last_frame_in_sequence = 0;
  195. if (!data) {
  196. /* push end of sequence if not already signalled */
  197. if (!p_dirac_params->eos_signalled) {
  198. dirac_encoder_end_sequence(p_dirac_params->p_encoder);
  199. p_dirac_params->eos_signalled = 1;
  200. }
  201. } else {
  202. /* Allocate frame data to Dirac input buffer.
  203. * Input line size may differ from what the codec supports,
  204. * especially when transcoding from one format to another.
  205. * So use avpicture_layout to copy the frame. */
  206. avpicture_layout((AVPicture *)data, avccontext->pix_fmt,
  207. avccontext->width, avccontext->height,
  208. p_dirac_params->p_in_frame_buf,
  209. p_dirac_params->frame_size);
  210. /* load next frame */
  211. if (dirac_encoder_load(p_dirac_params->p_encoder,
  212. p_dirac_params->p_in_frame_buf,
  213. p_dirac_params->frame_size) < 0) {
  214. av_log(avccontext, AV_LOG_ERROR, "Unrecoverable Encoder Error."
  215. " dirac_encoder_load failed...\n");
  216. return -1;
  217. }
  218. }
  219. if (p_dirac_params->eos_pulled)
  220. go = 0;
  221. while (go) {
  222. p_dirac_params->p_encoder->enc_buf.buffer = frame;
  223. p_dirac_params->p_encoder->enc_buf.size = buf_size;
  224. /* process frame */
  225. state = dirac_encoder_output(p_dirac_params->p_encoder);
  226. switch (state) {
  227. case ENC_STATE_AVAIL:
  228. case ENC_STATE_EOS:
  229. assert(p_dirac_params->p_encoder->enc_buf.size > 0);
  230. /* All non-frame data is prepended to actual frame data to
  231. * be able to set the pts correctly. So we don't write data
  232. * to the frame output queue until we actually have a frame
  233. */
  234. p_dirac_params->enc_buf = av_realloc(p_dirac_params->enc_buf,
  235. p_dirac_params->enc_buf_size +
  236. p_dirac_params->p_encoder->enc_buf.size);
  237. memcpy(p_dirac_params->enc_buf + p_dirac_params->enc_buf_size,
  238. p_dirac_params->p_encoder->enc_buf.buffer,
  239. p_dirac_params->p_encoder->enc_buf.size);
  240. p_dirac_params->enc_buf_size += p_dirac_params->p_encoder->enc_buf.size;
  241. if (state == ENC_STATE_EOS) {
  242. p_dirac_params->eos_pulled = 1;
  243. go = 0;
  244. }
  245. /* If non-frame data, don't output it until it we get an
  246. * encoded frame back from the encoder. */
  247. if (p_dirac_params->p_encoder->enc_pparams.pnum == -1)
  248. break;
  249. /* create output frame */
  250. p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame));
  251. /* set output data */
  252. p_frame_output->size = p_dirac_params->enc_buf_size;
  253. p_frame_output->p_encbuf = p_dirac_params->enc_buf;
  254. p_frame_output->frame_num = p_dirac_params->p_encoder->enc_pparams.pnum;
  255. if (p_dirac_params->p_encoder->enc_pparams.ptype == INTRA_PICTURE &&
  256. p_dirac_params->p_encoder->enc_pparams.rtype == REFERENCE_PICTURE)
  257. p_frame_output->key_frame = 1;
  258. ff_dirac_schro_queue_push_back(&p_dirac_params->enc_frame_queue,
  259. p_frame_output);
  260. p_dirac_params->enc_buf_size = 0;
  261. p_dirac_params->enc_buf = NULL;
  262. break;
  263. case ENC_STATE_BUFFER:
  264. go = 0;
  265. break;
  266. case ENC_STATE_INVALID:
  267. av_log(avccontext, AV_LOG_ERROR,
  268. "Unrecoverable Dirac Encoder Error. Quitting...\n");
  269. return -1;
  270. default:
  271. av_log(avccontext, AV_LOG_ERROR, "Unknown Dirac Encoder state\n");
  272. return -1;
  273. }
  274. }
  275. /* copy 'next' frame in queue */
  276. if (p_dirac_params->enc_frame_queue.size == 1 && p_dirac_params->eos_pulled)
  277. last_frame_in_sequence = 1;
  278. p_next_output_frame = ff_dirac_schro_queue_pop(&p_dirac_params->enc_frame_queue);
  279. if (!p_next_output_frame)
  280. return 0;
  281. memcpy(frame, p_next_output_frame->p_encbuf, p_next_output_frame->size);
  282. avccontext->coded_frame->key_frame = p_next_output_frame->key_frame;
  283. /* Use the frame number of the encoded frame as the pts. It is OK to do
  284. * so since Dirac is a constant framerate codec. It expects input to be
  285. * of constant framerate. */
  286. avccontext->coded_frame->pts = p_next_output_frame->frame_num;
  287. enc_size = p_next_output_frame->size;
  288. /* Append the end of sequence information to the last frame in the
  289. * sequence. */
  290. if (last_frame_in_sequence && p_dirac_params->enc_buf_size > 0) {
  291. memcpy(frame + enc_size, p_dirac_params->enc_buf,
  292. p_dirac_params->enc_buf_size);
  293. enc_size += p_dirac_params->enc_buf_size;
  294. av_freep(&p_dirac_params->enc_buf);
  295. p_dirac_params->enc_buf_size = 0;
  296. }
  297. /* free frame */
  298. DiracFreeFrame(p_next_output_frame);
  299. return enc_size;
  300. }
  301. static av_cold int libdirac_encode_close(AVCodecContext *avccontext)
  302. {
  303. FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data;
  304. /* close the encoder */
  305. dirac_encoder_close(p_dirac_params->p_encoder);
  306. /* free data in the output frame queue */
  307. ff_dirac_schro_queue_free(&p_dirac_params->enc_frame_queue,
  308. DiracFreeFrame);
  309. /* free the encoder buffer */
  310. if (p_dirac_params->enc_buf_size)
  311. av_freep(&p_dirac_params->enc_buf);
  312. /* free the input frame buffer */
  313. av_freep(&p_dirac_params->p_in_frame_buf);
  314. return 0;
  315. }
  316. AVCodec libdirac_encoder = {
  317. "libdirac",
  318. CODEC_TYPE_VIDEO,
  319. CODEC_ID_DIRAC,
  320. sizeof(FfmpegDiracEncoderParams),
  321. libdirac_encode_init,
  322. libdirac_encode_frame,
  323. libdirac_encode_close,
  324. .capabilities = CODEC_CAP_DELAY,
  325. .pix_fmts = (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE},
  326. .long_name = NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),
  327. };