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.

134 lines
3.3KB

  1. /**
  2. Copyright (C) 2005 Måns Rullgård
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  16. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  17. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. DEALINGS IN THE SOFTWARE.
  20. **/
  21. #include <stdlib.h>
  22. #include <theora/theora.h>
  23. #include "avcodec.h"
  24. typedef struct TheoraContext {
  25. theora_info info;
  26. theora_state state;
  27. theora_comment comment;
  28. ogg_packet op;
  29. } TheoraContext;
  30. static int
  31. Theora_decode_frame(AVCodecContext *ctx, void *outdata, int *outdata_size,
  32. uint8_t *buf, int buf_size)
  33. {
  34. TheoraContext *thc = ctx->priv_data;
  35. AVFrame *frame = outdata;
  36. yuv_buffer yuv;
  37. thc->op.packet = buf;
  38. thc->op.bytes = buf_size;
  39. if(theora_decode_packetin(&thc->state, &thc->op))
  40. return -1;
  41. theora_decode_YUVout(&thc->state, &yuv);
  42. frame->data[0] = yuv.y;
  43. frame->data[1] = yuv.u;
  44. frame->data[2] = yuv.v;
  45. frame->linesize[0] = yuv.y_stride;
  46. frame->linesize[1] = yuv.uv_stride;
  47. frame->linesize[2] = yuv.uv_stride;
  48. *outdata_size = sizeof(*frame);
  49. return buf_size;
  50. }
  51. static int
  52. Theora_decode_end(AVCodecContext *ctx)
  53. {
  54. TheoraContext *thc = ctx->priv_data;
  55. theora_info_clear(&thc->info);
  56. theora_comment_clear(&thc->comment);
  57. return 0;
  58. }
  59. static int
  60. Theora_decode_init(AVCodecContext *ctx)
  61. {
  62. TheoraContext *thc = ctx->priv_data;
  63. int size, hs, i;
  64. ogg_packet op;
  65. uint8_t *cdp;
  66. if(ctx->extradata_size < 6)
  67. return -1;
  68. theora_info_init(&thc->info);
  69. memset(&op, 0, sizeof(op));
  70. cdp = ctx->extradata;
  71. size = ctx->extradata_size;
  72. for(i = 0; i < 3; i++){
  73. hs = *cdp++ << 8;
  74. hs += *cdp++;
  75. size -= 2;
  76. if(hs > size){
  77. av_log(ctx, AV_LOG_ERROR, "extradata too small: %i > %i\n",
  78. hs, size);
  79. return -1;
  80. }
  81. op.packet = cdp;
  82. op.bytes = hs;
  83. op.b_o_s = !i;
  84. if(theora_decode_header(&thc->info, &thc->comment, &op))
  85. return -1;
  86. op.packetno++;
  87. cdp += hs;
  88. size -= hs;
  89. }
  90. theora_decode_init(&thc->state, &thc->info);
  91. ctx->width = thc->info.width;
  92. ctx->height = thc->info.height;
  93. ctx->time_base.num = thc->info.fps_denominator;
  94. ctx->time_base.den = thc->info.fps_numerator;
  95. ctx->pix_fmt = PIX_FMT_YUV420P; /* FIXME: others are possible */
  96. return 0;
  97. }
  98. AVCodec oggtheora_decoder = {
  99. "theora",
  100. CODEC_TYPE_VIDEO,
  101. CODEC_ID_THEORA,
  102. sizeof(TheoraContext),
  103. Theora_decode_init,
  104. NULL,
  105. Theora_decode_end,
  106. Theora_decode_frame,
  107. 0,
  108. NULL
  109. };