oggparsetheora.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. Copyright (C) 2005 Matthieu CASTET, Alex Beregszaszi
  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 "libavutil/bswap.h"
  23. #include "libavcodec/bitstream.h"
  24. #include "avformat.h"
  25. #include "oggdec.h"
  26. struct theora_params {
  27. int gpshift;
  28. int gpmask;
  29. unsigned version;
  30. };
  31. static int
  32. theora_header (AVFormatContext * s, int idx)
  33. {
  34. struct ogg *ogg = s->priv_data;
  35. struct ogg_stream *os = ogg->streams + idx;
  36. AVStream *st = s->streams[idx];
  37. struct theora_params *thp = os->private;
  38. int cds = st->codec->extradata_size + os->psize + 2;
  39. uint8_t *cdp;
  40. if(!(os->buf[os->pstart] & 0x80))
  41. return 0;
  42. if(!thp){
  43. thp = av_mallocz(sizeof(*thp));
  44. os->private = thp;
  45. }
  46. if (os->buf[os->pstart] == 0x80) {
  47. GetBitContext gb;
  48. int width, height;
  49. init_get_bits(&gb, os->buf + os->pstart, os->psize*8);
  50. skip_bits(&gb, 7*8); /* 0x80"theora" */
  51. thp->version = get_bits_long(&gb, 24);
  52. if (thp->version < 0x030100)
  53. {
  54. av_log(s, AV_LOG_ERROR,
  55. "Too old or unsupported Theora (%x)\n", thp->version);
  56. return -1;
  57. }
  58. width = get_bits(&gb, 16) << 4;
  59. height = get_bits(&gb, 16) << 4;
  60. avcodec_set_dimensions(st->codec, width, height);
  61. if (thp->version >= 0x030400)
  62. skip_bits(&gb, 100);
  63. if (thp->version >= 0x030200) {
  64. width = get_bits_long(&gb, 24);
  65. height = get_bits_long(&gb, 24);
  66. if ( width <= st->codec->width && width > st->codec->width-16
  67. && height <= st->codec->height && height > st->codec->height-16)
  68. avcodec_set_dimensions(st->codec, width, height);
  69. skip_bits(&gb, 16);
  70. }
  71. st->codec->time_base.den = get_bits_long(&gb, 32);
  72. st->codec->time_base.num = get_bits_long(&gb, 32);
  73. st->time_base = st->codec->time_base;
  74. st->sample_aspect_ratio.num = get_bits_long(&gb, 24);
  75. st->sample_aspect_ratio.den = get_bits_long(&gb, 24);
  76. if (thp->version >= 0x030200)
  77. skip_bits(&gb, 38);
  78. if (thp->version >= 0x304000)
  79. skip_bits(&gb, 2);
  80. thp->gpshift = get_bits(&gb, 5);
  81. thp->gpmask = (1 << thp->gpshift) - 1;
  82. st->codec->codec_type = CODEC_TYPE_VIDEO;
  83. st->codec->codec_id = CODEC_ID_THEORA;
  84. } else if (os->buf[os->pstart] == 0x83) {
  85. vorbis_comment (s, os->buf + os->pstart + 7, os->psize - 8);
  86. }
  87. st->codec->extradata = av_realloc (st->codec->extradata, cds);
  88. cdp = st->codec->extradata + st->codec->extradata_size;
  89. *cdp++ = os->psize >> 8;
  90. *cdp++ = os->psize & 0xff;
  91. memcpy (cdp, os->buf + os->pstart, os->psize);
  92. st->codec->extradata_size = cds;
  93. return 1;
  94. }
  95. static uint64_t
  96. theora_gptopts(AVFormatContext *ctx, int idx, uint64_t gp)
  97. {
  98. struct ogg *ogg = ctx->priv_data;
  99. struct ogg_stream *os = ogg->streams + idx;
  100. struct theora_params *thp = os->private;
  101. uint64_t iframe = gp >> thp->gpshift;
  102. uint64_t pframe = gp & thp->gpmask;
  103. if (thp->version < 0x030201)
  104. iframe++;
  105. if(!pframe)
  106. os->pflags |= PKT_FLAG_KEY;
  107. return iframe + pframe;
  108. }
  109. const struct ogg_codec ff_theora_codec = {
  110. .magic = "\200theora",
  111. .magicsize = 7,
  112. .header = theora_header,
  113. .gptopts = theora_gptopts
  114. };