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.

178 lines
5.4KB

  1. /**
  2. * Copyright (C) 2005 Matthieu CASTET, Alex Beregszaszi
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. **/
  24. #include <stdlib.h>
  25. #include "libavutil/bswap.h"
  26. #include "libavcodec/bitstream.h"
  27. #include "avformat.h"
  28. #include "internal.h"
  29. #include "oggdec.h"
  30. typedef struct TheoraParams {
  31. int gpshift;
  32. int gpmask;
  33. unsigned version;
  34. } TheoraParams;
  35. static int theora_header(AVFormatContext *s, int idx)
  36. {
  37. struct ogg *ogg = s->priv_data;
  38. struct ogg_stream *os = ogg->streams + idx;
  39. AVStream *st = s->streams[idx];
  40. TheoraParams *thp = os->private;
  41. int cds = st->codecpar->extradata_size + os->psize + 2;
  42. int err;
  43. uint8_t *cdp;
  44. if (!(os->buf[os->pstart] & 0x80))
  45. return 0;
  46. if (!thp) {
  47. thp = av_mallocz(sizeof(*thp));
  48. if (!thp)
  49. return AVERROR(ENOMEM);
  50. os->private = thp;
  51. }
  52. switch (os->buf[os->pstart]) {
  53. case 0x80: {
  54. BitstreamContext bc;
  55. AVRational timebase;
  56. bitstream_init8(&bc, os->buf + os->pstart, os->psize);
  57. /* 0x80"theora" */
  58. bitstream_skip(&bc, 7 * 8);
  59. thp->version = bitstream_read(&bc, 24);
  60. if (thp->version < 0x030100) {
  61. av_log(s, AV_LOG_ERROR,
  62. "Too old or unsupported Theora (%x)\n", thp->version);
  63. return AVERROR(ENOSYS);
  64. }
  65. st->codecpar->width = bitstream_read(&bc, 16) << 4;
  66. st->codecpar->height = bitstream_read(&bc, 16) << 4;
  67. if (thp->version >= 0x030400)
  68. bitstream_skip(&bc, 100);
  69. if (thp->version >= 0x030200) {
  70. int width = bitstream_read(&bc, 24);
  71. int height = bitstream_read(&bc, 24);
  72. if (width <= st->codecpar->width && width > st->codecpar->width - 16 &&
  73. height <= st->codecpar->height && height > st->codecpar->height - 16) {
  74. st->codecpar->width = width;
  75. st->codecpar->height = height;
  76. }
  77. bitstream_skip(&bc, 16);
  78. }
  79. timebase.den = bitstream_read(&bc, 32);
  80. timebase.num = bitstream_read(&bc, 32);
  81. if (!(timebase.num > 0 && timebase.den > 0)) {
  82. av_log(s, AV_LOG_WARNING, "Invalid time base in theora stream, assuming 25 FPS\n");
  83. timebase.num = 1;
  84. timebase.den = 25;
  85. }
  86. avpriv_set_pts_info(st, 64, timebase.num, timebase.den);
  87. st->sample_aspect_ratio.num = bitstream_read(&bc, 24);
  88. st->sample_aspect_ratio.den = bitstream_read(&bc, 24);
  89. if (thp->version >= 0x030200)
  90. bitstream_skip(&bc, 38);
  91. if (thp->version >= 0x304000)
  92. bitstream_skip(&bc, 2);
  93. thp->gpshift = bitstream_read(&bc, 5);
  94. thp->gpmask = (1 << thp->gpshift) - 1;
  95. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  96. st->codecpar->codec_id = AV_CODEC_ID_THEORA;
  97. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  98. }
  99. break;
  100. case 0x81:
  101. ff_vorbis_stream_comment(s, st, os->buf + os->pstart + 7, os->psize - 7);
  102. case 0x82:
  103. if (!thp->version)
  104. return AVERROR_INVALIDDATA;
  105. break;
  106. default:
  107. return AVERROR_INVALIDDATA;
  108. }
  109. if ((err = av_reallocp(&st->codecpar->extradata,
  110. cds + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
  111. st->codecpar->extradata_size = 0;
  112. return err;
  113. }
  114. cdp = st->codecpar->extradata + st->codecpar->extradata_size;
  115. *cdp++ = os->psize >> 8;
  116. *cdp++ = os->psize & 0xff;
  117. memcpy(cdp, os->buf + os->pstart, os->psize);
  118. st->codecpar->extradata_size = cds;
  119. return 1;
  120. }
  121. static uint64_t theora_gptopts(AVFormatContext *ctx, int idx, uint64_t gp,
  122. int64_t *dts)
  123. {
  124. struct ogg *ogg = ctx->priv_data;
  125. struct ogg_stream *os = ogg->streams + idx;
  126. TheoraParams *thp = os->private;
  127. uint64_t iframe, pframe;
  128. if (!thp)
  129. return AV_NOPTS_VALUE;
  130. iframe = gp >> thp->gpshift;
  131. pframe = gp & thp->gpmask;
  132. if (thp->version < 0x030201)
  133. iframe++;
  134. if (!pframe)
  135. os->pflags |= AV_PKT_FLAG_KEY;
  136. if (dts)
  137. *dts = iframe + pframe;
  138. return iframe + pframe;
  139. }
  140. const struct ogg_codec ff_theora_codec = {
  141. .magic = "\200theora",
  142. .magicsize = 7,
  143. .header = theora_header,
  144. .gptopts = theora_gptopts,
  145. .nb_header = 3,
  146. };