oggdec.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /*
  2. * Ogg bitstream support
  3. * Luca Barbato <lu_zero@gentoo.org>
  4. * Based on tcvp implementation
  5. */
  6. /*
  7. Copyright (C) 2005 Michael Ahlberg, Måns Rullgård
  8. Permission is hereby granted, free of charge, to any person
  9. obtaining a copy of this software and associated documentation
  10. files (the "Software"), to deal in the Software without
  11. restriction, including without limitation the rights to use, copy,
  12. modify, merge, publish, distribute, sublicense, and/or sell copies
  13. of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be
  16. included in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. DEALINGS IN THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include "libavutil/avassert.h"
  28. #include "oggdec.h"
  29. #include "avformat.h"
  30. #include "internal.h"
  31. #include "vorbiscomment.h"
  32. #define MAX_PAGE_SIZE 65307
  33. #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
  34. static const struct ogg_codec * const ogg_codecs[] = {
  35. &ff_skeleton_codec,
  36. &ff_dirac_codec,
  37. &ff_speex_codec,
  38. &ff_vorbis_codec,
  39. &ff_theora_codec,
  40. &ff_flac_codec,
  41. &ff_celt_codec,
  42. &ff_old_dirac_codec,
  43. &ff_old_flac_codec,
  44. &ff_ogm_video_codec,
  45. &ff_ogm_audio_codec,
  46. &ff_ogm_text_codec,
  47. &ff_ogm_old_codec,
  48. NULL
  49. };
  50. static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts);
  51. //FIXME We could avoid some structure duplication
  52. static int ogg_save(AVFormatContext *s)
  53. {
  54. struct ogg *ogg = s->priv_data;
  55. struct ogg_state *ost =
  56. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  57. int i;
  58. ost->pos = avio_tell (s->pb);
  59. ost->curidx = ogg->curidx;
  60. ost->next = ogg->state;
  61. ost->nstreams = ogg->nstreams;
  62. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  63. for (i = 0; i < ogg->nstreams; i++){
  64. struct ogg_stream *os = ogg->streams + i;
  65. os->buf = av_mallocz (os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  66. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  67. }
  68. ogg->state = ost;
  69. return 0;
  70. }
  71. static int ogg_restore(AVFormatContext *s, int discard)
  72. {
  73. struct ogg *ogg = s->priv_data;
  74. AVIOContext *bc = s->pb;
  75. struct ogg_state *ost = ogg->state;
  76. int i;
  77. if (!ost)
  78. return 0;
  79. ogg->state = ost->next;
  80. if (!discard){
  81. struct ogg_stream *old_streams = ogg->streams;
  82. for (i = 0; i < ogg->nstreams; i++)
  83. av_free (ogg->streams[i].buf);
  84. avio_seek (bc, ost->pos, SEEK_SET);
  85. ogg->curidx = ost->curidx;
  86. ogg->nstreams = ost->nstreams;
  87. ogg->streams = av_realloc (ogg->streams,
  88. ogg->nstreams * sizeof (*ogg->streams));
  89. if (ogg->streams) {
  90. memcpy(ogg->streams, ost->streams,
  91. ost->nstreams * sizeof(*ogg->streams));
  92. } else {
  93. av_free(old_streams);
  94. ogg->nstreams = 0;
  95. }
  96. }
  97. av_free (ost);
  98. return 0;
  99. }
  100. static int ogg_reset(AVFormatContext *s)
  101. {
  102. struct ogg *ogg = s->priv_data;
  103. int i;
  104. int64_t start_pos = avio_tell(s->pb);
  105. for (i = 0; i < ogg->nstreams; i++){
  106. struct ogg_stream *os = ogg->streams + i;
  107. os->bufpos = 0;
  108. os->pstart = 0;
  109. os->psize = 0;
  110. os->granule = -1;
  111. os->lastpts = AV_NOPTS_VALUE;
  112. os->lastdts = AV_NOPTS_VALUE;
  113. os->sync_pos = -1;
  114. os->page_pos = 0;
  115. os->nsegs = 0;
  116. os->segp = 0;
  117. os->incomplete = 0;
  118. if (start_pos <= s->data_offset) {
  119. os->lastpts = 0;
  120. }
  121. }
  122. ogg->curidx = -1;
  123. return 0;
  124. }
  125. static const struct ogg_codec *ogg_find_codec(uint8_t *buf, int size)
  126. {
  127. int i;
  128. for (i = 0; ogg_codecs[i]; i++)
  129. if (size >= ogg_codecs[i]->magicsize &&
  130. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  131. return ogg_codecs[i];
  132. return NULL;
  133. }
  134. static int ogg_new_stream(AVFormatContext *s, uint32_t serial, int new_avstream)
  135. {
  136. struct ogg *ogg = s->priv_data;
  137. int idx = ogg->nstreams++;
  138. AVStream *st;
  139. struct ogg_stream *os;
  140. ogg->streams = av_realloc (ogg->streams,
  141. ogg->nstreams * sizeof (*ogg->streams));
  142. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  143. os = ogg->streams + idx;
  144. os->serial = serial;
  145. os->bufsize = DECODER_BUFFER_SIZE;
  146. os->buf = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  147. os->header = -1;
  148. if (new_avstream) {
  149. st = avformat_new_stream(s, NULL);
  150. if (!st)
  151. return AVERROR(ENOMEM);
  152. st->id = idx;
  153. avpriv_set_pts_info(st, 64, 1, 1000000);
  154. }
  155. return idx;
  156. }
  157. static int ogg_new_buf(struct ogg *ogg, int idx)
  158. {
  159. struct ogg_stream *os = ogg->streams + idx;
  160. uint8_t *nb = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  161. int size = os->bufpos - os->pstart;
  162. if(os->buf){
  163. memcpy(nb, os->buf + os->pstart, size);
  164. av_free(os->buf);
  165. }
  166. os->buf = nb;
  167. os->bufpos = size;
  168. os->pstart = 0;
  169. return 0;
  170. }
  171. static int ogg_read_page(AVFormatContext *s, int *str)
  172. {
  173. AVIOContext *bc = s->pb;
  174. struct ogg *ogg = s->priv_data;
  175. struct ogg_stream *os;
  176. int ret, i = 0;
  177. int flags, nsegs;
  178. uint64_t gp;
  179. uint32_t serial;
  180. int size, idx;
  181. uint8_t sync[4];
  182. int sp = 0;
  183. ret = avio_read(bc, sync, 4);
  184. if (ret < 4)
  185. return ret < 0 ? ret : AVERROR_EOF;
  186. do{
  187. int c;
  188. if (sync[sp & 3] == 'O' &&
  189. sync[(sp + 1) & 3] == 'g' &&
  190. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  191. break;
  192. c = avio_r8(bc);
  193. if (url_feof(bc))
  194. return AVERROR_EOF;
  195. sync[sp++ & 3] = c;
  196. }while (i++ < MAX_PAGE_SIZE);
  197. if (i >= MAX_PAGE_SIZE){
  198. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  199. return AVERROR_INVALIDDATA;
  200. }
  201. if (avio_r8(bc) != 0){ /* version */
  202. av_log (s, AV_LOG_ERROR, "ogg page, unsupported version\n");
  203. return AVERROR_INVALIDDATA;
  204. }
  205. flags = avio_r8(bc);
  206. gp = avio_rl64 (bc);
  207. serial = avio_rl32 (bc);
  208. avio_skip(bc, 8); /* seq, crc */
  209. nsegs = avio_r8(bc);
  210. idx = ogg_find_stream (ogg, serial);
  211. if (idx < 0){
  212. if (ogg->headers) {
  213. int n;
  214. if (ogg->nstreams != 1) {
  215. av_log_missing_feature(s, "Changing stream parameters in multistream ogg is", 0);
  216. return idx;
  217. }
  218. for (n = 0; n < ogg->nstreams; n++) {
  219. av_freep(&ogg->streams[n].buf);
  220. if (!ogg->state || ogg->state->streams[n].private != ogg->streams[n].private)
  221. av_freep(&ogg->streams[n].private);
  222. }
  223. ogg->curidx = -1;
  224. ogg->nstreams = 0;
  225. idx = ogg_new_stream(s, serial, 0);
  226. } else {
  227. idx = ogg_new_stream(s, serial, 1);
  228. }
  229. if (idx < 0) {
  230. av_log (s, AV_LOG_ERROR, "failed to create stream (OOM?)\n");
  231. return idx;
  232. }
  233. }
  234. os = ogg->streams + idx;
  235. os->page_pos = avio_tell(bc) - 27;
  236. if(os->psize > 0)
  237. ogg_new_buf(ogg, idx);
  238. ret = avio_read(bc, os->segments, nsegs);
  239. if (ret < nsegs)
  240. return ret < 0 ? ret : AVERROR_EOF;
  241. os->nsegs = nsegs;
  242. os->segp = 0;
  243. size = 0;
  244. for (i = 0; i < nsegs; i++)
  245. size += os->segments[i];
  246. if (flags & OGG_FLAG_CONT || os->incomplete){
  247. if (!os->psize){
  248. // If this is the very first segment we started
  249. // playback in the middle of a continuation packet.
  250. // Discard it since we missed the start of it.
  251. while (os->segp < os->nsegs){
  252. int seg = os->segments[os->segp++];
  253. os->pstart += seg;
  254. if (seg < 255)
  255. break;
  256. }
  257. os->sync_pos = os->page_pos;
  258. }
  259. }else{
  260. os->psize = 0;
  261. os->sync_pos = os->page_pos;
  262. }
  263. if (os->bufsize - os->bufpos < size){
  264. uint8_t *nb = av_malloc ((os->bufsize *= 2) + FF_INPUT_BUFFER_PADDING_SIZE);
  265. memcpy (nb, os->buf, os->bufpos);
  266. av_free (os->buf);
  267. os->buf = nb;
  268. }
  269. ret = avio_read(bc, os->buf + os->bufpos, size);
  270. if (ret < size)
  271. return ret < 0 ? ret : AVERROR_EOF;
  272. os->bufpos += size;
  273. os->granule = gp;
  274. os->flags = flags;
  275. memset(os->buf + os->bufpos, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  276. if (str)
  277. *str = idx;
  278. return 0;
  279. }
  280. /**
  281. * @brief find the next Ogg packet
  282. * @param *str is set to the stream for the packet or -1 if there is
  283. * no matching stream, in that case assume all other return
  284. * values to be uninitialized.
  285. * @return negative value on error or EOF.
  286. */
  287. static int ogg_packet(AVFormatContext *s, int *str, int *dstart, int *dsize,
  288. int64_t *fpos)
  289. {
  290. struct ogg *ogg = s->priv_data;
  291. int idx, i, ret;
  292. struct ogg_stream *os;
  293. int complete = 0;
  294. int segp = 0, psize = 0;
  295. av_dlog(s, "ogg_packet: curidx=%i\n", ogg->curidx);
  296. if (str)
  297. *str = -1;
  298. do{
  299. idx = ogg->curidx;
  300. while (idx < 0){
  301. ret = ogg_read_page(s, &idx);
  302. if (ret < 0)
  303. return ret;
  304. }
  305. os = ogg->streams + idx;
  306. av_dlog(s, "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  307. idx, os->pstart, os->psize, os->segp, os->nsegs);
  308. if (!os->codec){
  309. if (os->header < 0){
  310. os->codec = ogg_find_codec (os->buf, os->bufpos);
  311. if (!os->codec){
  312. av_log(s, AV_LOG_WARNING, "Codec not found\n");
  313. os->header = 0;
  314. return 0;
  315. }
  316. }else{
  317. return 0;
  318. }
  319. }
  320. segp = os->segp;
  321. psize = os->psize;
  322. while (os->segp < os->nsegs){
  323. int ss = os->segments[os->segp++];
  324. os->psize += ss;
  325. if (ss < 255){
  326. complete = 1;
  327. break;
  328. }
  329. }
  330. if (!complete && os->segp == os->nsegs){
  331. ogg->curidx = -1;
  332. // Do not set incomplete for empty packets.
  333. // Together with the code in ogg_read_page
  334. // that discards all continuation of empty packets
  335. // we would get an infinite loop.
  336. os->incomplete = !!os->psize;
  337. }
  338. }while (!complete);
  339. if (os->granule == -1)
  340. av_log(s, AV_LOG_WARNING, "Page at %"PRId64" is missing granule\n", os->page_pos);
  341. ogg->curidx = idx;
  342. os->incomplete = 0;
  343. if (os->header) {
  344. os->header = os->codec->header (s, idx);
  345. if (!os->header){
  346. os->segp = segp;
  347. os->psize = psize;
  348. // We have reached the first non-header packet in this stream.
  349. // Unfortunately more header packets may still follow for others,
  350. // but if we continue with header parsing we may lose data packets.
  351. ogg->headers = 1;
  352. // Update the header state for all streams and
  353. // compute the data_offset.
  354. if (!s->data_offset)
  355. s->data_offset = os->sync_pos;
  356. for (i = 0; i < ogg->nstreams; i++) {
  357. struct ogg_stream *cur_os = ogg->streams + i;
  358. // if we have a partial non-header packet, its start is
  359. // obviously at or after the data start
  360. if (cur_os->incomplete)
  361. s->data_offset = FFMIN(s->data_offset, cur_os->sync_pos);
  362. }
  363. }else{
  364. os->pstart += os->psize;
  365. os->psize = 0;
  366. }
  367. } else {
  368. os->pflags = 0;
  369. os->pduration = 0;
  370. if (os->codec && os->codec->packet)
  371. os->codec->packet (s, idx);
  372. if (str)
  373. *str = idx;
  374. if (dstart)
  375. *dstart = os->pstart;
  376. if (dsize)
  377. *dsize = os->psize;
  378. if (fpos)
  379. *fpos = os->sync_pos;
  380. os->pstart += os->psize;
  381. os->psize = 0;
  382. if(os->pstart == os->bufpos)
  383. os->bufpos = os->pstart = 0;
  384. os->sync_pos = os->page_pos;
  385. }
  386. // determine whether there are more complete packets in this page
  387. // if not, the page's granule will apply to this packet
  388. os->page_end = 1;
  389. for (i = os->segp; i < os->nsegs; i++)
  390. if (os->segments[i] < 255) {
  391. os->page_end = 0;
  392. break;
  393. }
  394. if (os->segp == os->nsegs)
  395. ogg->curidx = -1;
  396. return 0;
  397. }
  398. static int ogg_get_headers(AVFormatContext *s)
  399. {
  400. struct ogg *ogg = s->priv_data;
  401. int ret;
  402. do{
  403. ret = ogg_packet(s, NULL, NULL, NULL, NULL);
  404. if (ret < 0)
  405. return ret;
  406. }while (!ogg->headers);
  407. av_dlog(s, "found headers\n");
  408. return 0;
  409. }
  410. static int ogg_get_length(AVFormatContext *s)
  411. {
  412. struct ogg *ogg = s->priv_data;
  413. int i;
  414. int64_t size, end;
  415. int streams_left=0;
  416. if(!s->pb->seekable)
  417. return 0;
  418. // already set
  419. if (s->duration != AV_NOPTS_VALUE)
  420. return 0;
  421. size = avio_size(s->pb);
  422. if(size < 0)
  423. return 0;
  424. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
  425. ogg_save (s);
  426. avio_seek (s->pb, end, SEEK_SET);
  427. while (!ogg_read_page (s, &i)){
  428. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  429. ogg->streams[i].codec) {
  430. s->streams[i]->duration =
  431. ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
  432. if (s->streams[i]->start_time != AV_NOPTS_VALUE){
  433. s->streams[i]->duration -= s->streams[i]->start_time;
  434. streams_left-= (ogg->streams[i].got_start==-1);
  435. ogg->streams[i].got_start= 1;
  436. }else if(!ogg->streams[i].got_start){
  437. ogg->streams[i].got_start= -1;
  438. streams_left++;
  439. }
  440. }
  441. }
  442. ogg_restore (s, 0);
  443. ogg_save (s);
  444. avio_seek (s->pb, s->data_offset, SEEK_SET);
  445. ogg_reset(s);
  446. while (streams_left > 0 && !ogg_packet(s, &i, NULL, NULL, NULL)) {
  447. int64_t pts;
  448. if (i < 0) continue;
  449. pts = ogg_calc_pts(s, i, NULL);
  450. if (pts != AV_NOPTS_VALUE && s->streams[i]->start_time == AV_NOPTS_VALUE && !ogg->streams[i].got_start){
  451. s->streams[i]->duration -= pts;
  452. ogg->streams[i].got_start= 1;
  453. streams_left--;
  454. }else if(s->streams[i]->start_time != AV_NOPTS_VALUE && !ogg->streams[i].got_start){
  455. ogg->streams[i].got_start= 1;
  456. streams_left--;
  457. }
  458. }
  459. ogg_restore (s, 0);
  460. return 0;
  461. }
  462. static int ogg_read_header(AVFormatContext *s)
  463. {
  464. struct ogg *ogg = s->priv_data;
  465. int ret, i;
  466. ogg->curidx = -1;
  467. //linear headers seek from start
  468. ret = ogg_get_headers(s);
  469. if (ret < 0)
  470. return ret;
  471. for (i = 0; i < ogg->nstreams; i++)
  472. if (ogg->streams[i].header < 0)
  473. ogg->streams[i].codec = NULL;
  474. //linear granulepos seek from end
  475. ogg_get_length (s);
  476. //fill the extradata in the per codec callbacks
  477. return 0;
  478. }
  479. static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
  480. {
  481. struct ogg *ogg = s->priv_data;
  482. struct ogg_stream *os = ogg->streams + idx;
  483. int64_t pts = AV_NOPTS_VALUE;
  484. if (dts)
  485. *dts = AV_NOPTS_VALUE;
  486. if (os->lastpts != AV_NOPTS_VALUE) {
  487. pts = os->lastpts;
  488. os->lastpts = AV_NOPTS_VALUE;
  489. }
  490. if (os->lastdts != AV_NOPTS_VALUE) {
  491. if (dts)
  492. *dts = os->lastdts;
  493. os->lastdts = AV_NOPTS_VALUE;
  494. }
  495. if (os->page_end) {
  496. if (os->granule != -1LL) {
  497. if (os->codec && os->codec->granule_is_start)
  498. pts = ogg_gptopts(s, idx, os->granule, dts);
  499. else
  500. os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
  501. os->granule = -1LL;
  502. }
  503. }
  504. return pts;
  505. }
  506. static void ogg_validate_keyframe(AVFormatContext *s, int idx, int pstart, int psize)
  507. {
  508. struct ogg *ogg = s->priv_data;
  509. struct ogg_stream *os = ogg->streams + idx;
  510. if (psize && s->streams[idx]->codec->codec_id == CODEC_ID_THEORA) {
  511. if (!!(os->pflags & AV_PKT_FLAG_KEY) != !(os->buf[pstart] & 0x40)) {
  512. os->pflags ^= AV_PKT_FLAG_KEY;
  513. av_log(s, AV_LOG_WARNING, "Broken file, %skeyframe not correctly marked.\n",
  514. (os->pflags & AV_PKT_FLAG_KEY) ? "" : "non-");
  515. }
  516. }
  517. }
  518. static int ogg_read_packet(AVFormatContext *s, AVPacket *pkt)
  519. {
  520. struct ogg *ogg;
  521. struct ogg_stream *os;
  522. int idx, ret;
  523. int pstart, psize;
  524. int64_t fpos, pts, dts;
  525. //Get an ogg packet
  526. retry:
  527. do{
  528. ret = ogg_packet(s, &idx, &pstart, &psize, &fpos);
  529. if (ret < 0)
  530. return ret;
  531. }while (idx < 0 || !s->streams[idx]);
  532. ogg = s->priv_data;
  533. os = ogg->streams + idx;
  534. // pflags might not be set until after this
  535. pts = ogg_calc_pts(s, idx, &dts);
  536. ogg_validate_keyframe(s, idx, pstart, psize);
  537. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  538. goto retry;
  539. os->keyframe_seek = 0;
  540. //Alloc a pkt
  541. ret = av_new_packet(pkt, psize);
  542. if (ret < 0)
  543. return ret;
  544. pkt->stream_index = idx;
  545. memcpy (pkt->data, os->buf + pstart, psize);
  546. pkt->pts = pts;
  547. pkt->dts = dts;
  548. pkt->flags = os->pflags;
  549. pkt->duration = os->pduration;
  550. pkt->pos = fpos;
  551. return psize;
  552. }
  553. static int ogg_read_close(AVFormatContext *s)
  554. {
  555. struct ogg *ogg = s->priv_data;
  556. int i;
  557. for (i = 0; i < ogg->nstreams; i++){
  558. av_free (ogg->streams[i].buf);
  559. av_free (ogg->streams[i].private);
  560. }
  561. av_free (ogg->streams);
  562. return 0;
  563. }
  564. static int64_t ogg_read_timestamp(AVFormatContext *s, int stream_index,
  565. int64_t *pos_arg, int64_t pos_limit)
  566. {
  567. struct ogg *ogg = s->priv_data;
  568. AVIOContext *bc = s->pb;
  569. int64_t pts = AV_NOPTS_VALUE;
  570. int64_t keypos = -1;
  571. int i;
  572. int pstart, psize;
  573. avio_seek(bc, *pos_arg, SEEK_SET);
  574. ogg_reset(s);
  575. while (avio_tell(bc) <= pos_limit && !ogg_packet(s, &i, &pstart, &psize, pos_arg)) {
  576. if (i == stream_index) {
  577. struct ogg_stream *os = ogg->streams + stream_index;
  578. pts = ogg_calc_pts(s, i, NULL);
  579. ogg_validate_keyframe(s, i, pstart, psize);
  580. if (os->pflags & AV_PKT_FLAG_KEY) {
  581. keypos = *pos_arg;
  582. } else if (os->keyframe_seek) {
  583. // if we had a previous keyframe but no pts for it,
  584. // return that keyframe with this pts value.
  585. if (keypos >= 0)
  586. *pos_arg = keypos;
  587. else
  588. pts = AV_NOPTS_VALUE;
  589. }
  590. }
  591. if (pts != AV_NOPTS_VALUE)
  592. break;
  593. }
  594. ogg_reset(s);
  595. return pts;
  596. }
  597. static int ogg_read_seek(AVFormatContext *s, int stream_index,
  598. int64_t timestamp, int flags)
  599. {
  600. struct ogg *ogg = s->priv_data;
  601. struct ogg_stream *os = ogg->streams + stream_index;
  602. int ret;
  603. av_assert0(stream_index < ogg->nstreams);
  604. // Ensure everything is reset even when seeking via
  605. // the generated index.
  606. ogg_reset(s);
  607. // Try seeking to a keyframe first. If this fails (very possible),
  608. // av_seek_frame will fall back to ignoring keyframes
  609. if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO
  610. && !(flags & AVSEEK_FLAG_ANY))
  611. os->keyframe_seek = 1;
  612. ret = ff_seek_frame_binary(s, stream_index, timestamp, flags);
  613. os = ogg->streams + stream_index;
  614. if (ret < 0)
  615. os->keyframe_seek = 0;
  616. return ret;
  617. }
  618. static int ogg_probe(AVProbeData *p)
  619. {
  620. if (!memcmp("OggS", p->buf, 5) && p->buf[5] <= 0x7)
  621. return AVPROBE_SCORE_MAX;
  622. return 0;
  623. }
  624. AVInputFormat ff_ogg_demuxer = {
  625. .name = "ogg",
  626. .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
  627. .priv_data_size = sizeof(struct ogg),
  628. .read_probe = ogg_probe,
  629. .read_header = ogg_read_header,
  630. .read_packet = ogg_read_packet,
  631. .read_close = ogg_read_close,
  632. .read_seek = ogg_read_seek,
  633. .read_timestamp = ogg_read_timestamp,
  634. .extensions = "ogg",
  635. .flags = AVFMT_GENERIC_INDEX,
  636. };