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.

289 lines
9.1KB

  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 Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*!
  21. * \file libtheoraenc.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 "libavutil/intreadwrite.h"
  33. #include "libavutil/log.h"
  34. #include "avcodec.h"
  35. /* libtheora includes */
  36. #include <theora/theora.h>
  37. typedef struct TheoraContext{
  38. theora_state t_state;
  39. } TheoraContext;
  40. /*!
  41. Concatenates an ogg_packet into the extradata.
  42. */
  43. static int concatenate_packet(unsigned int* offset, AVCodecContext* avc_context, const ogg_packet* packet)
  44. {
  45. const char* message = NULL;
  46. uint8_t* newdata = NULL;
  47. int newsize = avc_context->extradata_size + 2 + packet->bytes;
  48. if (packet->bytes < 0) {
  49. message = "ogg_packet has negative size";
  50. } else if (packet->bytes > 0xffff) {
  51. message = "ogg_packet is larger than 65535 bytes";
  52. } else if (newsize < avc_context->extradata_size) {
  53. message = "extradata_size would overflow";
  54. } else {
  55. newdata = av_realloc(avc_context->extradata, newsize);
  56. if (newdata == NULL) {
  57. message = "av_realloc failed";
  58. }
  59. }
  60. if (message != NULL) {
  61. av_log(avc_context, AV_LOG_ERROR, "concatenate_packet failed: %s\n", message);
  62. return -1;
  63. }
  64. avc_context->extradata = newdata;
  65. avc_context->extradata_size = newsize;
  66. AV_WB16(avc_context->extradata + (*offset), packet->bytes);
  67. *offset += 2;
  68. memcpy( avc_context->extradata + (*offset), packet->packet, packet->bytes );
  69. (*offset) += packet->bytes;
  70. return 0;
  71. }
  72. static av_cold int encode_init(AVCodecContext* avc_context)
  73. {
  74. theora_info t_info;
  75. theora_comment t_comment;
  76. ogg_packet o_packet;
  77. unsigned int offset;
  78. TheoraContext *h = avc_context->priv_data;
  79. /* Set up the theora_info struct */
  80. theora_info_init( &t_info );
  81. t_info.width = FFALIGN(avc_context->width, 16);
  82. t_info.height = FFALIGN(avc_context->height, 16);
  83. t_info.frame_width = avc_context->width;
  84. t_info.frame_height = avc_context->height;
  85. t_info.offset_x = 0;
  86. t_info.offset_y = avc_context->height & 0xf;
  87. /* Swap numerator and denominator as time_base in AVCodecContext gives the
  88. * time period between frames, but theora_info needs the framerate. */
  89. t_info.fps_numerator = avc_context->time_base.den;
  90. t_info.fps_denominator = avc_context->time_base.num;
  91. if (avc_context->sample_aspect_ratio.num != 0) {
  92. t_info.aspect_numerator = avc_context->sample_aspect_ratio.num;
  93. t_info.aspect_denominator = avc_context->sample_aspect_ratio.den;
  94. } else {
  95. t_info.aspect_numerator = 1;
  96. t_info.aspect_denominator = 1;
  97. }
  98. t_info.colorspace = OC_CS_UNSPECIFIED;
  99. t_info.pixelformat = OC_PF_420;
  100. t_info.target_bitrate = avc_context->bit_rate;
  101. t_info.keyframe_frequency = avc_context->gop_size;
  102. t_info.keyframe_frequency_force = avc_context->gop_size;
  103. t_info.keyframe_mindistance = avc_context->keyint_min;
  104. t_info.quality = 0;
  105. t_info.quick_p = 1;
  106. t_info.dropframes_p = 0;
  107. t_info.keyframe_auto_p = 1;
  108. t_info.keyframe_data_target_bitrate = t_info.target_bitrate * 1.5;
  109. t_info.keyframe_auto_threshold = 80;
  110. t_info.noise_sensitivity = 1;
  111. t_info.sharpness = 0;
  112. /* Now initialise libtheora */
  113. if (theora_encode_init( &(h->t_state), &t_info ) != 0) {
  114. av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n");
  115. return -1;
  116. }
  117. /* Clear up theora_info struct */
  118. theora_info_clear( &t_info );
  119. /*
  120. Output first header packet consisting of theora
  121. header, comment, and tables.
  122. Each one is prefixed with a 16bit size, then they
  123. are concatenated together into ffmpeg's extradata.
  124. */
  125. offset = 0;
  126. /* Header */
  127. theora_encode_header( &(h->t_state), &o_packet );
  128. if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) {
  129. return -1;
  130. }
  131. /* Comment */
  132. theora_comment_init( &t_comment );
  133. theora_encode_comment( &t_comment, &o_packet );
  134. if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) {
  135. return -1;
  136. }
  137. /* Clear up theora_comment struct before we reset the packet */
  138. theora_comment_clear( &t_comment );
  139. /* And despite documentation to the contrary, theora_comment_clear
  140. * does not release the packet */
  141. ogg_packet_clear(&o_packet);
  142. /* Tables */
  143. theora_encode_tables( &(h->t_state), &o_packet );
  144. if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) {
  145. return -1;
  146. }
  147. /* Set up the output AVFrame */
  148. avc_context->coded_frame= avcodec_alloc_frame();
  149. return 0;
  150. }
  151. static int encode_frame(
  152. AVCodecContext* avc_context,
  153. uint8_t *outbuf,
  154. int buf_size,
  155. void *data)
  156. {
  157. yuv_buffer t_yuv_buffer;
  158. TheoraContext *h = avc_context->priv_data;
  159. AVFrame *frame = data;
  160. ogg_packet o_packet;
  161. int result;
  162. assert(avc_context->pix_fmt == PIX_FMT_YUV420P);
  163. /* Copy planes to the theora yuv_buffer */
  164. if (frame->linesize[1] != frame->linesize[2]) {
  165. av_log(avc_context, AV_LOG_ERROR, "U and V stride differ\n");
  166. return -1;
  167. }
  168. t_yuv_buffer.y_width = FFALIGN(avc_context->width, 16);
  169. t_yuv_buffer.y_height = FFALIGN(avc_context->height, 16);
  170. t_yuv_buffer.y_stride = frame->linesize[0];
  171. t_yuv_buffer.uv_width = t_yuv_buffer.y_width / 2;
  172. t_yuv_buffer.uv_height = t_yuv_buffer.y_height / 2;
  173. t_yuv_buffer.uv_stride = frame->linesize[1];
  174. t_yuv_buffer.y = frame->data[0];
  175. t_yuv_buffer.u = frame->data[1];
  176. t_yuv_buffer.v = frame->data[2];
  177. /* Now call into theora_encode_YUVin */
  178. result = theora_encode_YUVin( &(h->t_state), &t_yuv_buffer );
  179. if (result != 0) {
  180. const char* message;
  181. switch (result) {
  182. case -1:
  183. message = "differing frame sizes";
  184. break;
  185. case OC_EINVAL:
  186. message = "encoder is not ready or is finished";
  187. break;
  188. default:
  189. message = "unknown reason";
  190. break;
  191. }
  192. av_log(avc_context, AV_LOG_ERROR, "theora_encode_YUVin failed (%s) [%d]\n", message, result);
  193. return -1;
  194. }
  195. /* Pick up returned ogg_packet */
  196. result = theora_encode_packetout( &(h->t_state), 0, &o_packet );
  197. switch (result) {
  198. case 0:
  199. /* No packet is ready */
  200. return 0;
  201. case 1:
  202. /* Success, we have a packet */
  203. break;
  204. default:
  205. av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed [%d]\n", result);
  206. return -1;
  207. }
  208. /* Copy ogg_packet content out to buffer */
  209. if (buf_size < o_packet.bytes) {
  210. av_log(avc_context, AV_LOG_ERROR, "encoded frame too large\n");
  211. return -1;
  212. }
  213. memcpy(outbuf, o_packet.packet, o_packet.bytes);
  214. return o_packet.bytes;
  215. }
  216. static av_cold int encode_close(AVCodecContext* avc_context)
  217. {
  218. ogg_packet o_packet;
  219. TheoraContext *h = avc_context->priv_data;
  220. int result;
  221. const char* message;
  222. result = theora_encode_packetout( &(h->t_state), 1, &o_packet );
  223. theora_clear( &(h->t_state) );
  224. av_freep(&avc_context->coded_frame);
  225. av_freep(&avc_context->extradata);
  226. avc_context->extradata_size = 0;
  227. switch (result) {
  228. case 0:/* No packet is ready */
  229. case -1:/* Encoding finished */
  230. return 0;
  231. case 1:
  232. /* We have a packet */
  233. message = "gave us a packet";
  234. break;
  235. default:
  236. message = "unknown reason";
  237. break;
  238. }
  239. av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed (%s) [%d]\n", message, result);
  240. return -1;
  241. }
  242. static const enum PixelFormat supported_pixel_formats[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
  243. /*! AVCodec struct exposed to libavcodec */
  244. AVCodec libtheora_encoder =
  245. {
  246. .name = "libtheora",
  247. .type = CODEC_TYPE_VIDEO,
  248. .id = CODEC_ID_THEORA,
  249. .priv_data_size = sizeof(TheoraContext),
  250. .init = encode_init,
  251. .close = encode_close,
  252. .encode = encode_frame,
  253. .pix_fmts = supported_pixel_formats,
  254. .long_name = NULL_IF_CONFIG_SMALL("libtheora Theora"),
  255. };