FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
oggparsevp8.c
Go to the documentation of this file.
1 /*
2  * On2 VP8 parser for Ogg
3  * Copyright (C) 2013 James Almer
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "oggdec.h"
26 
27 #define VP8_HEADER_SIZE 26
28 
29 static int vp8_header(AVFormatContext *s, int idx)
30 {
31  struct ogg *ogg = s->priv_data;
32  struct ogg_stream *os = ogg->streams + idx;
33  uint8_t *p = os->buf + os->pstart;
34  AVStream *st = s->streams[idx];
35  AVRational framerate;
36 
37  if (os->psize < 7 || p[0] != 0x4f)
38  return 0;
39 
40  switch (p[5]){
41  case 0x01:
42  if (os->psize < VP8_HEADER_SIZE) {
43  av_log(s, AV_LOG_ERROR, "Invalid OggVP8 header packet");
44  return AVERROR_INVALIDDATA;
45  }
46 
47  if (p[6] != 1) {
48  av_log(s, AV_LOG_WARNING, "Unknown OggVP8 version %d.%d\n", p[6], p[7]);
49  return AVERROR_INVALIDDATA;
50  }
51 
52  st->codec->width = AV_RB16(p + 8);
53  st->codec->height = AV_RB16(p + 10);
54  st->sample_aspect_ratio.num = AV_RB24(p + 12);
55  st->sample_aspect_ratio.den = AV_RB24(p + 15);
56  framerate.den = AV_RB32(p + 18);
57  framerate.num = AV_RB32(p + 22);
58 
59  avpriv_set_pts_info(st, 64, framerate.num, framerate.den);
63  break;
64  case 0x02:
65  if (p[6] != 0x20)
66  return AVERROR_INVALIDDATA;
67  ff_vorbis_comment(s, &st->metadata, p + 7, os->psize - 7, 1);
68  break;
69  default:
70  av_log(s, AV_LOG_ERROR, "Unknown VP8 header type 0x%02X\n", p[5]);
71  return AVERROR_INVALIDDATA;
72  }
73 
74  return 1;
75 }
76 
77 static uint64_t vp8_gptopts(AVFormatContext *s, int idx, uint64_t granule, int64_t *dts)
78 {
79  struct ogg *ogg = s->priv_data;
80  struct ogg_stream *os = ogg->streams + idx;
81 
82  uint64_t pts = (granule >> 32);
83  uint32_t dist = (granule >> 3) & 0x07ffffff;
84 
85  if (!dist)
86  os->pflags |= AV_PKT_FLAG_KEY;
87 
88  if (dts)
89  *dts = pts;
90 
91  return pts;
92 }
93 
94 static int vp8_packet(AVFormatContext *s, int idx)
95 {
96  struct ogg *ogg = s->priv_data;
97  struct ogg_stream *os = ogg->streams + idx;
98  uint8_t *p = os->buf + os->pstart;
99 
100  if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
101  int seg;
102  int duration;
103  uint8_t *last_pkt = p;
104  uint8_t *next_pkt;
105 
106  seg = os->segp;
107  duration = (last_pkt[0] >> 4) & 1;
108  next_pkt = last_pkt += os->psize;
109  for (; seg < os->nsegs; seg++) {
110  if (os->segments[seg] < 255) {
111  duration += (last_pkt[0] >> 4) & 1;
112  last_pkt = next_pkt + os->segments[seg];
113  }
114  next_pkt += os->segments[seg];
115  }
116  os->lastpts = os->lastdts = vp8_gptopts(s, idx, os->granule, NULL) - duration;
117  if(s->streams[idx]->start_time == AV_NOPTS_VALUE) {
118  s->streams[idx]->start_time = os->lastpts;
119  if (s->streams[idx]->duration)
120  s->streams[idx]->duration -= s->streams[idx]->start_time;
121  }
122  }
123 
124  if (os->psize > 0)
125  os->pduration = (p[0] >> 4) & 1;
126 
127  return 0;
128 }
129 
130 const struct ogg_codec ff_vp8_codec = {
131  .magic = "OVP80",
132  .magicsize = 5,
133  .header = vp8_header,
134  .packet = vp8_packet,
135  .gptopts = vp8_gptopts,
136  .nb_header = 1,
137 };