libtheoraenc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. 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 = avc_context->width;
  82. t_info.height = avc_context->height;
  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 = 0;
  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. /* Tables */
  138. theora_encode_tables( &(h->t_state), &o_packet );
  139. if (concatenate_packet( &offset, avc_context, &o_packet ) != 0) {
  140. return -1;
  141. }
  142. /* Clear up theora_comment struct */
  143. theora_comment_clear( &t_comment );
  144. /* Set up the output AVFrame */
  145. avc_context->coded_frame= avcodec_alloc_frame();
  146. return 0;
  147. }
  148. static int encode_frame(
  149. AVCodecContext* avc_context,
  150. uint8_t *outbuf,
  151. int buf_size,
  152. void *data)
  153. {
  154. yuv_buffer t_yuv_buffer;
  155. TheoraContext *h = avc_context->priv_data;
  156. AVFrame *frame = data;
  157. ogg_packet o_packet;
  158. int result;
  159. assert(avc_context->pix_fmt == PIX_FMT_YUV420P);
  160. /* Copy planes to the theora yuv_buffer */
  161. if (frame->linesize[1] != frame->linesize[2]) {
  162. av_log(avc_context, AV_LOG_ERROR, "U and V stride differ\n");
  163. return -1;
  164. }
  165. t_yuv_buffer.y_width = avc_context->width;
  166. t_yuv_buffer.y_height = avc_context->height;
  167. t_yuv_buffer.y_stride = frame->linesize[0];
  168. t_yuv_buffer.uv_width = t_yuv_buffer.y_width / 2;
  169. t_yuv_buffer.uv_height = t_yuv_buffer.y_height / 2;
  170. t_yuv_buffer.uv_stride = frame->linesize[1];
  171. t_yuv_buffer.y = frame->data[0];
  172. t_yuv_buffer.u = frame->data[1];
  173. t_yuv_buffer.v = frame->data[2];
  174. /* Now call into theora_encode_YUVin */
  175. result = theora_encode_YUVin( &(h->t_state), &t_yuv_buffer );
  176. if (result != 0) {
  177. const char* message;
  178. switch (result) {
  179. case -1:
  180. message = "differing frame sizes";
  181. break;
  182. case OC_EINVAL:
  183. message = "encoder is not ready or is finished";
  184. break;
  185. default:
  186. message = "unknown reason";
  187. break;
  188. }
  189. av_log(avc_context, AV_LOG_ERROR, "theora_encode_YUVin failed (%s) [%d]\n", message, result);
  190. return -1;
  191. }
  192. /* Pick up returned ogg_packet */
  193. result = theora_encode_packetout( &(h->t_state), 0, &o_packet );
  194. switch (result) {
  195. case 0:
  196. /* No packet is ready */
  197. return 0;
  198. case 1:
  199. /* Success, we have a packet */
  200. break;
  201. default:
  202. av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed [%d]\n", result);
  203. return -1;
  204. }
  205. /* Copy ogg_packet content out to buffer */
  206. if (buf_size < o_packet.bytes) {
  207. av_log(avc_context, AV_LOG_ERROR, "encoded frame too large\n");
  208. return -1;
  209. }
  210. memcpy(outbuf, o_packet.packet, o_packet.bytes);
  211. return o_packet.bytes;
  212. }
  213. static av_cold int encode_close(AVCodecContext* avc_context)
  214. {
  215. ogg_packet o_packet;
  216. TheoraContext *h = avc_context->priv_data;
  217. int result;
  218. const char* message;
  219. result = theora_encode_packetout( &(h->t_state), 1, &o_packet );
  220. theora_clear( &(h->t_state) );
  221. switch (result) {
  222. case 0:/* No packet is ready */
  223. case -1:/* Encoding finished */
  224. return 0;
  225. case 1:
  226. /* We have a packet */
  227. message = "gave us a packet";
  228. break;
  229. default:
  230. message = "unknown reason";
  231. break;
  232. }
  233. av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed (%s) [%d]\n", message, result);
  234. return -1;
  235. }
  236. static const enum PixelFormat supported_pixel_formats[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
  237. /*! AVCodec struct exposed to libavcodec */
  238. AVCodec libtheora_encoder =
  239. {
  240. .name = "libtheora",
  241. .type = CODEC_TYPE_VIDEO,
  242. .id = CODEC_ID_THEORA,
  243. .priv_data_size = sizeof(TheoraContext),
  244. .init = encode_init,
  245. .close = encode_close,
  246. .encode = encode_frame,
  247. .pix_fmts = supported_pixel_formats,
  248. .long_name = NULL_IF_CONFIG_SMALL("libtheora Theora"),
  249. };