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.

297 lines
9.7KB

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