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.

281 lines
8.6KB

  1. /*
  2. * Copyright (c) 2006 Paul Richards <paul.richards@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*!
  21. * \file theoraenc.c
  22. * \brief Theora encoder using libtheora.
  23. * \author Paul Richards <paul.richards@gmail.com>
  24. *
  25. * A lot of this is copy / paste from other output codecs in
  26. * libavcodec or pure guesswork (or both).
  27. *
  28. * I have used t_ prefixes on variables which are libtheora types
  29. * and o_ prefixes on variables which are libogg types.
  30. */
  31. /* FFmpeg includes */
  32. #include "avcodec.h"
  33. #include "log.h"
  34. /* libtheora includes */
  35. #include <theora/theora.h>
  36. typedef struct TheoraContext{
  37. theora_state t_state;
  38. } TheoraContext;
  39. /*!
  40. Concatenates an ogg_packet into the extradata.
  41. */
  42. static int concatenate_packet(unsigned int* offset, AVCodecContext* avc_context, const ogg_packet* packet)
  43. {
  44. char* message = NULL;
  45. uint8_t* newdata = NULL;
  46. int newsize = avc_context->extradata_size + 2 + packet->bytes;
  47. if (packet->bytes < 0) {
  48. message = "ogg_packet has negative size";
  49. } else if (packet->bytes > 0xffff) {
  50. message = "ogg_packet is larger than 65535 bytes";
  51. } else if (newsize < avc_context->extradata_size) {
  52. message = "extradata_size would overflow";
  53. } else {
  54. newdata = av_realloc(avc_context->extradata, newsize);
  55. if (newdata == NULL) {
  56. message = "av_realloc failed";
  57. }
  58. }
  59. if (message != NULL) {
  60. av_log(avc_context, AV_LOG_ERROR, "concatenate_packet failed: %s\n", message);
  61. return -1;
  62. }
  63. avc_context->extradata = newdata;
  64. avc_context->extradata_size = newsize;
  65. avc_context->extradata[ (*offset)++ ] = packet->bytes >> 8;
  66. avc_context->extradata[ (*offset)++ ] = packet->bytes & 0xff;
  67. memcpy( avc_context->extradata + (*offset), packet->packet, packet->bytes );
  68. (*offset) += packet->bytes;
  69. return 0;
  70. }
  71. static int encode_init(AVCodecContext* avc_context)
  72. {
  73. theora_info t_info;
  74. theora_comment t_comment;
  75. ogg_packet o_packet;
  76. unsigned int offset;
  77. TheoraContext *h = avc_context->priv_data;
  78. /* Set up the theora_info struct */
  79. theora_info_init( &t_info );
  80. t_info.width = avc_context->width;
  81. t_info.height = avc_context->height;
  82. t_info.frame_width = avc_context->width;
  83. t_info.frame_height = avc_context->height;
  84. t_info.offset_x = 0;
  85. t_info.offset_y = 0;
  86. /* Swap numerator and denominator as time_base in AVCodecContext gives the
  87. * time period between frames, but theora_info needs the framerate. */
  88. t_info.fps_numerator = avc_context->time_base.den;
  89. t_info.fps_denominator = avc_context->time_base.num;
  90. if (avc_context->sample_aspect_ratio.num != 0) {
  91. t_info.aspect_numerator = avc_context->sample_aspect_ratio.num;
  92. t_info.aspect_denominator = avc_context->sample_aspect_ratio.den;
  93. } else {
  94. t_info.aspect_numerator = 1;
  95. t_info.aspect_denominator = 1;
  96. }
  97. t_info.colorspace = OC_CS_UNSPECIFIED;
  98. t_info.pixelformat = OC_PF_420;
  99. t_info.target_bitrate = avc_context->bit_rate;
  100. t_info.keyframe_frequency = avc_context->gop_size;
  101. t_info.keyframe_frequency_force = avc_context->gop_size;
  102. t_info.keyframe_mindistance = avc_context->keyint_min;
  103. t_info.quality = 0;
  104. t_info.quick_p = 1;
  105. t_info.dropframes_p = 0;
  106. t_info.keyframe_auto_p = 1;
  107. t_info.keyframe_data_target_bitrate = t_info.target_bitrate * 1.5;
  108. t_info.keyframe_auto_threshold = 80;
  109. t_info.noise_sensitivity = 1;
  110. t_info.sharpness = 0;
  111. /* Now initialise libtheora */
  112. if (theora_encode_init( &(h->t_state), &t_info ) != 0) {
  113. av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n");
  114. return -1;
  115. }
  116. /* Clear up theora_info struct */
  117. theora_info_clear( &t_info );
  118. /*
  119. Output first header packet consisting of theora
  120. header, comment, and tables.
  121. Each one is prefixed with a 16bit size, then they
  122. are concatenated together into ffmpeg's extradata.
  123. */
  124. offset = 0;
  125. /* Header */
  126. theora_encode_header( &(h->t_state), &o_packet );
  127. if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) {
  128. return -1;
  129. }
  130. /* Comment */
  131. theora_comment_init( &t_comment );
  132. theora_encode_comment( &t_comment, &o_packet );
  133. if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) {
  134. return -1;
  135. }
  136. /* Tables */
  137. theora_encode_tables( &(h->t_state), &o_packet );
  138. if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) {
  139. return -1;
  140. }
  141. /* Clear up theora_comment struct */
  142. theora_comment_clear( &t_comment );
  143. /* Set up the output AVFrame */
  144. avc_context->coded_frame= avcodec_alloc_frame();
  145. return 0;
  146. }
  147. static int encode_frame(
  148. AVCodecContext* avc_context,
  149. uint8_t *outbuf,
  150. int buf_size,
  151. void *data)
  152. {
  153. yuv_buffer t_yuv_buffer;
  154. TheoraContext *h = avc_context->priv_data;
  155. AVFrame *frame = data;
  156. ogg_packet o_packet;
  157. int result;
  158. assert(avc_context->pix_fmt == PIX_FMT_YUV420P);
  159. /* Copy planes to the theora yuv_buffer */
  160. if (frame->linesize[1] != frame->linesize[2]) {
  161. av_log(avc_context, AV_LOG_ERROR, "U and V stride differ\n");
  162. return -1;
  163. }
  164. t_yuv_buffer.y_width = avc_context->width;
  165. t_yuv_buffer.y_height = avc_context->height;
  166. t_yuv_buffer.y_stride = frame->linesize[0];
  167. t_yuv_buffer.uv_width = t_yuv_buffer.y_width / 2;
  168. t_yuv_buffer.uv_height = t_yuv_buffer.y_height / 2;
  169. t_yuv_buffer.uv_stride = frame->linesize[1];
  170. t_yuv_buffer.y = frame->data[0];
  171. t_yuv_buffer.u = frame->data[1];
  172. t_yuv_buffer.v = frame->data[2];
  173. /* Now call into theora_encode_YUVin */
  174. result = theora_encode_YUVin( &(h->t_state), &t_yuv_buffer );
  175. if (result != 0) {
  176. const char* message;
  177. switch (result) {
  178. case -1:
  179. message = "differing frame sizes";
  180. break;
  181. case OC_EINVAL:
  182. message = "encoder is not ready or is finished";
  183. break;
  184. default:
  185. message = "unknown reason";
  186. break;
  187. }
  188. av_log(avc_context, AV_LOG_ERROR, "theora_encode_YUVin failed (%s) [%d]\n", message, result);
  189. return -1;
  190. }
  191. /* Pick up returned ogg_packet */
  192. result = theora_encode_packetout( &(h->t_state), 0, &o_packet );
  193. switch (result) {
  194. case 0:
  195. /* No packet is ready */
  196. return 0;
  197. case 1:
  198. /* Success, we have a packet */
  199. break;
  200. default:
  201. av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed [%d]\n", result);
  202. return -1;
  203. }
  204. /* Copy ogg_packet content out to buffer */
  205. if (buf_size < o_packet.bytes) {
  206. av_log(avc_context, AV_LOG_ERROR, "encoded frame too large\n");
  207. return -1;
  208. }
  209. memcpy(outbuf, o_packet.packet, o_packet.bytes);
  210. return o_packet.bytes;
  211. }
  212. static int encode_close(AVCodecContext* avc_context)
  213. {
  214. ogg_packet o_packet;
  215. TheoraContext *h = avc_context->priv_data;
  216. int result;
  217. const char* message;
  218. result = theora_encode_packetout( &(h->t_state), 1, &o_packet );
  219. theora_clear( &(h->t_state) );
  220. switch (result) {
  221. case 0:/* No packet is ready */
  222. case -1:/* Encoding finished */
  223. return 0;
  224. case 1:
  225. /* We have a packet */
  226. message = "gave us a packet";
  227. break;
  228. default:
  229. message = "unknown reason";
  230. break;
  231. }
  232. av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed (%s) [%d]\n", message, result);
  233. return -1;
  234. }
  235. static const enum PixelFormat supported_pixel_formats[] = { PIX_FMT_YUV420P, -1 };
  236. /*! AVCodec struct exposed to libavcodec */
  237. AVCodec libtheora_encoder =
  238. {
  239. .name = "libtheora",
  240. .type = CODEC_TYPE_VIDEO,
  241. .id = CODEC_ID_THEORA,
  242. .priv_data_size = sizeof(TheoraContext),
  243. .init = encode_init,
  244. .close = encode_close,
  245. .encode = encode_frame,
  246. .pix_fmts = supported_pixel_formats,
  247. };