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.

299 lines
9.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 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. 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. AV_WB16(avc_context->extradata + (*offset), packet->bytes);
  66. *offset += 2;
  67. memcpy( avc_context->extradata + (*offset), packet->packet, packet->bytes );
  68. (*offset) += packet->bytes;
  69. return 0;
  70. }
  71. static av_cold 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 = FFALIGN(avc_context->width, 16);
  81. t_info.height = FFALIGN(avc_context->height, 16);
  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 = avc_context->height & 0xf;
  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.keyframe_frequency = avc_context->gop_size;
  100. t_info.keyframe_frequency_force = avc_context->gop_size;
  101. t_info.keyframe_mindistance = avc_context->keyint_min;
  102. t_info.quick_p = 1;
  103. t_info.dropframes_p = 0;
  104. t_info.keyframe_auto_p = 1;
  105. t_info.keyframe_data_target_bitrate = t_info.target_bitrate * 1.5;
  106. t_info.keyframe_auto_threshold = 80;
  107. t_info.noise_sensitivity = 1;
  108. t_info.sharpness = 0;
  109. if (avc_context->flags & CODEC_FLAG_QSCALE) {
  110. /* to be constant with the libvorbis implementation, clip global_quality to 0 - 10
  111. Theora accepts a quality parameter p, which is:
  112. * 0 <= p <=63
  113. * an int value
  114. */
  115. t_info.quality = av_clip(avc_context->global_quality / (float)FF_QP2LAMBDA, 0, 10) * 6.3;
  116. t_info.target_bitrate = 0;
  117. } else {
  118. t_info.target_bitrate = avc_context->bit_rate;
  119. t_info.quality = 0;
  120. }
  121. /* Now initialise libtheora */
  122. if (theora_encode_init( &(h->t_state), &t_info ) != 0) {
  123. av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n");
  124. return -1;
  125. }
  126. /* Clear up theora_info struct */
  127. theora_info_clear( &t_info );
  128. /*
  129. Output first header packet consisting of theora
  130. header, comment, and tables.
  131. Each one is prefixed with a 16bit size, then they
  132. are concatenated together into ffmpeg's extradata.
  133. */
  134. offset = 0;
  135. /* Header */
  136. theora_encode_header( &(h->t_state), &o_packet );
  137. if (concatenate_packet( &offset, avc_context, &o_packet ) != 0)
  138. return -1;
  139. /* Comment */
  140. theora_comment_init( &t_comment );
  141. theora_encode_comment( &t_comment, &o_packet );
  142. if (concatenate_packet( &offset, avc_context, &o_packet ) != 0)
  143. return -1;
  144. /* Clear up theora_comment struct before we reset the packet */
  145. theora_comment_clear( &t_comment );
  146. /* And despite documentation to the contrary, theora_comment_clear
  147. * does not release the packet */
  148. ogg_packet_clear(&o_packet);
  149. /* Tables */
  150. theora_encode_tables( &(h->t_state), &o_packet );
  151. if (concatenate_packet( &offset, avc_context, &o_packet ) != 0)
  152. return -1;
  153. /* Set up the output AVFrame */
  154. avc_context->coded_frame= avcodec_alloc_frame();
  155. return 0;
  156. }
  157. static int encode_frame(
  158. AVCodecContext* avc_context,
  159. uint8_t *outbuf,
  160. int buf_size,
  161. void *data)
  162. {
  163. yuv_buffer t_yuv_buffer;
  164. TheoraContext *h = avc_context->priv_data;
  165. AVFrame *frame = data;
  166. ogg_packet o_packet;
  167. int result;
  168. assert(avc_context->pix_fmt == PIX_FMT_YUV420P);
  169. /* Copy planes to the theora yuv_buffer */
  170. if (frame->linesize[1] != frame->linesize[2]) {
  171. av_log(avc_context, AV_LOG_ERROR, "U and V stride differ\n");
  172. return -1;
  173. }
  174. t_yuv_buffer.y_width = FFALIGN(avc_context->width, 16);
  175. t_yuv_buffer.y_height = FFALIGN(avc_context->height, 16);
  176. t_yuv_buffer.y_stride = frame->linesize[0];
  177. t_yuv_buffer.uv_width = t_yuv_buffer.y_width / 2;
  178. t_yuv_buffer.uv_height = t_yuv_buffer.y_height / 2;
  179. t_yuv_buffer.uv_stride = frame->linesize[1];
  180. t_yuv_buffer.y = frame->data[0];
  181. t_yuv_buffer.u = frame->data[1];
  182. t_yuv_buffer.v = frame->data[2];
  183. /* Now call into theora_encode_YUVin */
  184. result = theora_encode_YUVin( &(h->t_state), &t_yuv_buffer );
  185. if (result != 0) {
  186. const char* message;
  187. switch (result) {
  188. case -1:
  189. message = "differing frame sizes";
  190. break;
  191. case OC_EINVAL:
  192. message = "encoder is not ready or is finished";
  193. break;
  194. default:
  195. message = "unknown reason";
  196. break;
  197. }
  198. av_log(avc_context, AV_LOG_ERROR, "theora_encode_YUVin failed (%s) [%d]\n", message, result);
  199. return -1;
  200. }
  201. /* Pick up returned ogg_packet */
  202. result = theora_encode_packetout( &(h->t_state), 0, &o_packet );
  203. switch (result) {
  204. case 0:
  205. /* No packet is ready */
  206. return 0;
  207. case 1:
  208. /* Success, we have a packet */
  209. break;
  210. default:
  211. av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed [%d]\n", result);
  212. return -1;
  213. }
  214. /* Copy ogg_packet content out to buffer */
  215. if (buf_size < o_packet.bytes) {
  216. av_log(avc_context, AV_LOG_ERROR, "encoded frame too large\n");
  217. return -1;
  218. }
  219. memcpy(outbuf, o_packet.packet, o_packet.bytes);
  220. // HACK: does not take codec delay into account (neither does the decoder though)
  221. avc_context->coded_frame->pts= frame->pts;
  222. return o_packet.bytes;
  223. }
  224. static av_cold int encode_close(AVCodecContext* avc_context)
  225. {
  226. ogg_packet o_packet;
  227. TheoraContext *h = avc_context->priv_data;
  228. int result;
  229. const char* message;
  230. result = theora_encode_packetout( &(h->t_state), 1, &o_packet );
  231. theora_clear( &(h->t_state) );
  232. av_freep(&avc_context->coded_frame);
  233. av_freep(&avc_context->extradata);
  234. avc_context->extradata_size = 0;
  235. switch (result) {
  236. case 0:/* No packet is ready */
  237. case -1:/* Encoding finished */
  238. return 0;
  239. case 1:
  240. /* We have a packet */
  241. message = "gave us a packet";
  242. break;
  243. default:
  244. message = "unknown reason";
  245. break;
  246. }
  247. av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed (%s) [%d]\n", message, result);
  248. return -1;
  249. }
  250. static const enum PixelFormat supported_pixel_formats[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
  251. /*! AVCodec struct exposed to libavcodec */
  252. AVCodec libtheora_encoder =
  253. {
  254. .name = "libtheora",
  255. .type = CODEC_TYPE_VIDEO,
  256. .id = CODEC_ID_THEORA,
  257. .priv_data_size = sizeof(TheoraContext),
  258. .init = encode_init,
  259. .close = encode_close,
  260. .encode = encode_frame,
  261. .pix_fmts = supported_pixel_formats,
  262. .long_name = NULL_IF_CONFIG_SMALL("libtheora Theora"),
  263. };