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.

375 lines
13KB

  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 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. {
  37. /** Dirac encoder context */
  38. dirac_encoder_context_t enc_ctx;
  39. /** frame being encoded */
  40. AVFrame picture;
  41. /** frame size */
  42. int frame_size;
  43. /** Dirac encoder handle */
  44. dirac_encoder_t* p_encoder;
  45. /** input frame buffer */
  46. unsigned char *p_in_frame_buf;
  47. /** queue storing encoded frames */
  48. FfmpegDiracSchroQueue enc_frame_queue;
  49. /** end of sequence signalled by user, 0 - false, 1 - true */
  50. int eos_signalled;
  51. /** end of sequence returned by encoder, 0 - false, 1 - true */
  52. int eos_pulled;
  53. } FfmpegDiracEncoderParams;
  54. /**
  55. * Works out Dirac-compatible chroma format.
  56. */
  57. static dirac_chroma_t GetDiracChromaFormat(enum PixelFormat ff_pix_fmt)
  58. {
  59. int num_formats = sizeof(ffmpeg_dirac_pixel_format_map) /
  60. sizeof(ffmpeg_dirac_pixel_format_map[0]);
  61. int idx;
  62. for (idx = 0; idx < num_formats; ++idx) {
  63. if (ffmpeg_dirac_pixel_format_map[idx].ff_pix_fmt == ff_pix_fmt) {
  64. return ffmpeg_dirac_pixel_format_map[idx].dirac_pix_fmt;
  65. }
  66. }
  67. return formatNK;
  68. }
  69. /**
  70. * Dirac video preset table. Ensure that this tables matches up correctly
  71. * with the ff_dirac_schro_video_format_info table in libdirac_libschro.c.
  72. */
  73. static const VideoFormat ff_dirac_video_formats[]={
  74. VIDEO_FORMAT_CUSTOM ,
  75. VIDEO_FORMAT_QSIF525 ,
  76. VIDEO_FORMAT_QCIF ,
  77. VIDEO_FORMAT_SIF525 ,
  78. VIDEO_FORMAT_CIF ,
  79. VIDEO_FORMAT_4SIF525 ,
  80. VIDEO_FORMAT_4CIF ,
  81. VIDEO_FORMAT_SD_480I60 ,
  82. VIDEO_FORMAT_SD_576I50 ,
  83. VIDEO_FORMAT_HD_720P60 ,
  84. VIDEO_FORMAT_HD_720P50 ,
  85. VIDEO_FORMAT_HD_1080I60 ,
  86. VIDEO_FORMAT_HD_1080I50 ,
  87. VIDEO_FORMAT_HD_1080P60 ,
  88. VIDEO_FORMAT_HD_1080P50 ,
  89. VIDEO_FORMAT_DIGI_CINEMA_2K24 ,
  90. VIDEO_FORMAT_DIGI_CINEMA_4K24 ,
  91. };
  92. /**
  93. * Returns the video format preset matching the input video dimensions and
  94. * time base.
  95. */
  96. static VideoFormat GetDiracVideoFormatPreset (AVCodecContext *avccontext)
  97. {
  98. unsigned int num_formats = sizeof(ff_dirac_video_formats) /
  99. sizeof(ff_dirac_video_formats[0]);
  100. unsigned int idx = ff_dirac_schro_get_video_format_idx (avccontext);
  101. return (idx < num_formats) ?
  102. ff_dirac_video_formats[idx] : VIDEO_FORMAT_CUSTOM;
  103. }
  104. static int libdirac_encode_init(AVCodecContext *avccontext)
  105. {
  106. FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data;
  107. int no_local = 1;
  108. int verbose = avccontext->debug;
  109. VideoFormat preset;
  110. /* get Dirac preset */
  111. preset = GetDiracVideoFormatPreset(avccontext);
  112. /* initialize the encoder context */
  113. dirac_encoder_context_init (&(p_dirac_params->enc_ctx), preset);
  114. p_dirac_params->enc_ctx.src_params.chroma =
  115. 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 =
  124. avccontext->time_base.den;
  125. p_dirac_params->enc_ctx.src_params.frame_rate.denominator =
  126. avccontext->time_base.num;
  127. p_dirac_params->enc_ctx.src_params.width = avccontext->width;
  128. p_dirac_params->enc_ctx.src_params.height = avccontext->height;
  129. p_dirac_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
  130. avccontext->width,
  131. avccontext->height);
  132. avccontext->coded_frame = &p_dirac_params->picture;
  133. if (no_local) {
  134. p_dirac_params->enc_ctx.decode_flag = 0;
  135. p_dirac_params->enc_ctx.instr_flag = 0;
  136. } else {
  137. p_dirac_params->enc_ctx.decode_flag = 1;
  138. p_dirac_params->enc_ctx.instr_flag = 1;
  139. }
  140. /* Intra-only sequence */
  141. if (avccontext->gop_size == 0 )
  142. p_dirac_params->enc_ctx.enc_params.num_L1 = 0;
  143. else
  144. avccontext->has_b_frames = 1;
  145. if (avccontext->flags & CODEC_FLAG_QSCALE) {
  146. if (avccontext->global_quality != 0) {
  147. p_dirac_params->enc_ctx.enc_params.qf =
  148. avccontext->global_quality / (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 =
  153. avccontext->bit_rate / 1000;
  154. }
  155. } else
  156. p_dirac_params->enc_ctx.enc_params.lossless = 1;
  157. } else if (avccontext->bit_rate >= 1000)
  158. p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate / 1000;
  159. if ((preset > VIDEO_FORMAT_QCIF || preset < VIDEO_FORMAT_QSIF525) &&
  160. avccontext->bit_rate == 200000) {
  161. p_dirac_params->enc_ctx.enc_params.trate = 0;
  162. }
  163. if (avccontext->flags & CODEC_FLAG_INTERLACED_ME) {
  164. /* all material can be coded as interlaced or progressive
  165. * irrespective of the type of source material */
  166. p_dirac_params->enc_ctx.enc_params.picture_coding_mode = 1;
  167. }
  168. p_dirac_params->p_encoder = dirac_encoder_init (&(p_dirac_params->enc_ctx),
  169. verbose );
  170. if (!p_dirac_params->p_encoder) {
  171. av_log(avccontext, AV_LOG_ERROR,
  172. "Unrecoverable Error: dirac_encoder_init failed. ");
  173. return EXIT_FAILURE;
  174. }
  175. /* allocate enough memory for the incoming data */
  176. p_dirac_params->p_in_frame_buf = av_malloc(p_dirac_params->frame_size);
  177. /* initialize the encoded frame queue */
  178. ff_dirac_schro_queue_init(&p_dirac_params->enc_frame_queue);
  179. return 0 ;
  180. }
  181. static void DiracFreeFrame (void *data)
  182. {
  183. FfmpegDiracSchroEncodedFrame *enc_frame = data;
  184. av_freep (&(enc_frame->p_encbuf));
  185. av_free(enc_frame);
  186. }
  187. static int libdirac_encode_frame(AVCodecContext *avccontext,
  188. unsigned char *frame,
  189. int buf_size, void *data)
  190. {
  191. int enc_size = 0;
  192. dirac_encoder_state_t state;
  193. FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data;
  194. FfmpegDiracSchroEncodedFrame* p_frame_output = NULL;
  195. FfmpegDiracSchroEncodedFrame* p_next_output_frame = NULL;
  196. int go = 1;
  197. if (data == NULL) {
  198. /* push end of sequence if not already signalled */
  199. if (!p_dirac_params->eos_signalled) {
  200. dirac_encoder_end_sequence( p_dirac_params->p_encoder );
  201. p_dirac_params->eos_signalled = 1;
  202. }
  203. } else {
  204. /* Allocate frame data to Dirac input buffer.
  205. * Input line size may differ from what the codec supports,
  206. * especially when transcoding from one format to another.
  207. * So use avpicture_layout to copy the frame. */
  208. avpicture_layout ((AVPicture *)data, avccontext->pix_fmt,
  209. avccontext->width, avccontext->height,
  210. p_dirac_params->p_in_frame_buf,
  211. p_dirac_params->frame_size);
  212. /* load next frame */
  213. if (dirac_encoder_load (p_dirac_params->p_encoder,
  214. p_dirac_params->p_in_frame_buf,
  215. p_dirac_params->frame_size ) < 0) {
  216. av_log(avccontext, AV_LOG_ERROR, "Unrecoverable Encoder Error."
  217. " dirac_encoder_load failed...\n");
  218. return -1;
  219. }
  220. }
  221. if (p_dirac_params->eos_pulled)
  222. go = 0;
  223. while(go) {
  224. p_dirac_params->p_encoder->enc_buf.buffer = frame;
  225. p_dirac_params->p_encoder->enc_buf.size = buf_size;
  226. /* process frame */
  227. state = dirac_encoder_output ( p_dirac_params->p_encoder );
  228. switch (state)
  229. {
  230. case ENC_STATE_AVAIL:
  231. case ENC_STATE_EOS:
  232. assert (p_dirac_params->p_encoder->enc_buf.size > 0);
  233. /* create output frame */
  234. p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame));
  235. /* set output data */
  236. p_frame_output->p_encbuf =
  237. av_malloc(p_dirac_params->p_encoder->enc_buf.size);
  238. memcpy(p_frame_output->p_encbuf,
  239. p_dirac_params->p_encoder->enc_buf.buffer,
  240. p_dirac_params->p_encoder->enc_buf.size);
  241. p_frame_output->size = p_dirac_params->p_encoder->enc_buf.size;
  242. p_frame_output->frame_num =
  243. p_dirac_params->p_encoder->enc_pparams.pnum;
  244. if (p_dirac_params->p_encoder->enc_pparams.ptype == INTRA_PICTURE &&
  245. p_dirac_params->p_encoder->enc_pparams.rtype == REFERENCE_PICTURE)
  246. p_frame_output->key_frame = 1;
  247. ff_dirac_schro_queue_push_back (&p_dirac_params->enc_frame_queue,
  248. p_frame_output);
  249. if (state == ENC_STATE_EOS) {
  250. p_dirac_params->eos_pulled = 1;
  251. go = 0;
  252. }
  253. break;
  254. case ENC_STATE_BUFFER:
  255. go = 0;
  256. break;
  257. case ENC_STATE_INVALID:
  258. av_log(avccontext, AV_LOG_ERROR,
  259. "Unrecoverable Dirac Encoder Error. Quitting...\n");
  260. return -1;
  261. default:
  262. av_log(avccontext, AV_LOG_ERROR, "Unknown Dirac Encoder state\n");
  263. return -1;
  264. }
  265. }
  266. /* copy 'next' frame in queue */
  267. p_next_output_frame =
  268. ff_dirac_schro_queue_pop(&p_dirac_params->enc_frame_queue);
  269. if (p_next_output_frame == NULL)
  270. return 0;
  271. memcpy(frame, p_next_output_frame->p_encbuf, p_next_output_frame->size);
  272. avccontext->coded_frame->key_frame = p_next_output_frame->key_frame;
  273. /* Use the frame number of the encoded frame as the pts. It is OK to do
  274. * so since Dirac is a constant framerate codec. It expects input to be
  275. * of constant framerate. */
  276. avccontext->coded_frame->pts = p_next_output_frame->frame_num;
  277. enc_size = p_next_output_frame->size;
  278. /* free frame */
  279. DiracFreeFrame(p_next_output_frame);
  280. return enc_size;
  281. }
  282. static int libdirac_encode_close(AVCodecContext *avccontext)
  283. {
  284. FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data;
  285. /* close the encoder */
  286. dirac_encoder_close(p_dirac_params->p_encoder );
  287. /* free data in the output frame queue */
  288. ff_dirac_schro_queue_free(&p_dirac_params->enc_frame_queue,
  289. DiracFreeFrame);
  290. /* free the input frame buffer */
  291. av_freep(&p_dirac_params->p_in_frame_buf);
  292. return 0 ;
  293. }
  294. AVCodec libdirac_encoder = {
  295. "libdirac",
  296. CODEC_TYPE_VIDEO,
  297. CODEC_ID_DIRAC,
  298. sizeof(FfmpegDiracEncoderParams),
  299. libdirac_encode_init,
  300. libdirac_encode_frame,
  301. libdirac_encode_close,
  302. .capabilities= CODEC_CAP_DELAY,
  303. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, -1},
  304. .long_name= NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),
  305. } ;