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.

273 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 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/theoraenc.h>
  37. typedef struct TheoraContext {
  38. th_enc_ctx *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. th_info t_info;
  76. th_comment t_comment;
  77. ogg_packet o_packet;
  78. unsigned int offset;
  79. TheoraContext *h = avc_context->priv_data;
  80. uint32_t gop_size = avc_context->gop_size;
  81. /* Set up the theora_info struct */
  82. th_info_init(&t_info);
  83. t_info.frame_width = FFALIGN(avc_context->width, 16);
  84. t_info.frame_height = FFALIGN(avc_context->height, 16);
  85. t_info.pic_width = avc_context->width;
  86. t_info.pic_height = avc_context->height;
  87. t_info.pic_x = 0;
  88. t_info.pic_y = 0;
  89. /* Swap numerator and denominator as time_base in AVCodecContext gives the
  90. * time period between frames, but theora_info needs the framerate. */
  91. t_info.fps_numerator = avc_context->time_base.den;
  92. t_info.fps_denominator = avc_context->time_base.num;
  93. if (avc_context->sample_aspect_ratio.num) {
  94. t_info.aspect_numerator = avc_context->sample_aspect_ratio.num;
  95. t_info.aspect_denominator = avc_context->sample_aspect_ratio.den;
  96. } else {
  97. t_info.aspect_numerator = 1;
  98. t_info.aspect_denominator = 1;
  99. }
  100. t_info.colorspace = TH_CS_UNSPECIFIED;
  101. t_info.pixel_fmt = TH_PF_420;
  102. if (avc_context->flags & CODEC_FLAG_QSCALE) {
  103. /* to be constant with the libvorbis implementation, clip global_quality to 0 - 10
  104. Theora accepts a quality parameter p, which is:
  105. * 0 <= p <=63
  106. * an int value
  107. */
  108. t_info.quality = av_clip(avc_context->global_quality / (float)FF_QP2LAMBDA, 0, 10) * 6.3;
  109. t_info.target_bitrate = 0;
  110. } else {
  111. t_info.target_bitrate = avc_context->bit_rate;
  112. t_info.quality = 0;
  113. }
  114. /* Now initialise libtheora */
  115. h->t_state = th_encode_alloc(&t_info);
  116. if (!h->t_state) {
  117. av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n");
  118. return -1;
  119. }
  120. /* Clear up theora_info struct */
  121. th_info_clear(&t_info);
  122. if (th_encode_ctl(h->t_state, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
  123. &gop_size, sizeof(gop_size))) {
  124. av_log(avc_context, AV_LOG_ERROR, "Error setting GOP size\n");
  125. return -1;
  126. }
  127. /*
  128. Output first header packet consisting of theora
  129. header, comment, and tables.
  130. Each one is prefixed with a 16bit size, then they
  131. are concatenated together into ffmpeg's extradata.
  132. */
  133. offset = 0;
  134. /* Headers */
  135. th_comment_init(&t_comment);
  136. while (th_encode_flushheader(h->t_state, &t_comment, &o_packet))
  137. if (concatenate_packet(&offset, avc_context, &o_packet))
  138. return -1;
  139. th_comment_clear(&t_comment);
  140. /* Set up the output AVFrame */
  141. avc_context->coded_frame= avcodec_alloc_frame();
  142. return 0;
  143. }
  144. static int encode_frame(AVCodecContext* avc_context, uint8_t *outbuf,
  145. int buf_size, void *data)
  146. {
  147. th_ycbcr_buffer t_yuv_buffer;
  148. TheoraContext *h = avc_context->priv_data;
  149. AVFrame *frame = data;
  150. ogg_packet o_packet;
  151. int result, i;
  152. assert(avc_context->pix_fmt == PIX_FMT_YUV420P);
  153. /* Copy planes to the theora yuv_buffer */
  154. for (i = 0; i < 3; i++) {
  155. t_yuv_buffer[i].width = FFALIGN(avc_context->width, 16) >> !!i;
  156. t_yuv_buffer[i].height = FFALIGN(avc_context->height, 16) >> !!i;
  157. t_yuv_buffer[i].stride = frame->linesize[i];
  158. t_yuv_buffer[i].data = frame->data[i];
  159. }
  160. /* Now call into theora_encode_YUVin */
  161. result = th_encode_ycbcr_in(h->t_state, t_yuv_buffer);
  162. if (result) {
  163. const char* message;
  164. switch (result) {
  165. case -1:
  166. message = "differing frame sizes";
  167. break;
  168. case TH_EINVAL:
  169. message = "encoder is not ready or is finished";
  170. break;
  171. default:
  172. message = "unknown reason";
  173. break;
  174. }
  175. av_log(avc_context, AV_LOG_ERROR, "theora_encode_YUVin failed (%s) [%d]\n", message, result);
  176. return -1;
  177. }
  178. /* Pick up returned ogg_packet */
  179. result = th_encode_packetout(h->t_state, 0, &o_packet);
  180. switch (result) {
  181. case 0:
  182. /* No packet is ready */
  183. return 0;
  184. case 1:
  185. /* Success, we have a packet */
  186. break;
  187. default:
  188. av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed [%d]\n", result);
  189. return -1;
  190. }
  191. /* Copy ogg_packet content out to buffer */
  192. if (buf_size < o_packet.bytes) {
  193. av_log(avc_context, AV_LOG_ERROR, "encoded frame too large\n");
  194. return -1;
  195. }
  196. memcpy(outbuf, o_packet.packet, o_packet.bytes);
  197. // HACK: does not take codec delay into account (neither does the decoder though)
  198. avc_context->coded_frame->pts = frame->pts;
  199. return o_packet.bytes;
  200. }
  201. static av_cold int encode_close(AVCodecContext* avc_context)
  202. {
  203. ogg_packet o_packet;
  204. TheoraContext *h = avc_context->priv_data;
  205. int result;
  206. const char* message;
  207. result = th_encode_packetout(h->t_state, 1, &o_packet);
  208. th_encode_free(h->t_state);
  209. av_freep(&avc_context->coded_frame);
  210. av_freep(&avc_context->extradata);
  211. avc_context->extradata_size = 0;
  212. switch (result) {
  213. case 0: /* No packet is ready */
  214. case -1: /* Encoding finished */
  215. return 0;
  216. case 1:
  217. /* We have a packet */
  218. message = "gave us a packet";
  219. break;
  220. default:
  221. message = "unknown reason";
  222. break;
  223. }
  224. av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed (%s) [%d]\n", message, result);
  225. return -1;
  226. }
  227. static const enum PixelFormat supported_pixel_formats[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
  228. /*! AVCodec struct exposed to libavcodec */
  229. AVCodec libtheora_encoder = {
  230. .name = "libtheora",
  231. .type = CODEC_TYPE_VIDEO,
  232. .id = CODEC_ID_THEORA,
  233. .priv_data_size = sizeof(TheoraContext),
  234. .init = encode_init,
  235. .close = encode_close,
  236. .encode = encode_frame,
  237. .pix_fmts = supported_pixel_formats,
  238. .long_name = NULL_IF_CONFIG_SMALL("libtheora Theora"),
  239. };