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.

873 lines
24KB

  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 "libavutil/intreadwrite.h"
  29. #include "oggdec.h"
  30. #include "avformat.h"
  31. #include "internal.h"
  32. #include "vorbiscomment.h"
  33. #define MAX_PAGE_SIZE 65307
  34. #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
  35. static const struct ogg_codec * const ogg_codecs[] = {
  36. &ff_skeleton_codec,
  37. &ff_dirac_codec,
  38. &ff_speex_codec,
  39. &ff_vorbis_codec,
  40. &ff_theora_codec,
  41. &ff_flac_codec,
  42. &ff_celt_codec,
  43. &ff_opus_codec,
  44. &ff_old_dirac_codec,
  45. &ff_old_flac_codec,
  46. &ff_ogm_video_codec,
  47. &ff_ogm_audio_codec,
  48. &ff_ogm_text_codec,
  49. &ff_ogm_old_codec,
  50. NULL
  51. };
  52. static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts);
  53. static int ogg_new_stream(AVFormatContext *s, uint32_t serial);
  54. //FIXME We could avoid some structure duplication
  55. static int ogg_save(AVFormatContext *s)
  56. {
  57. struct ogg *ogg = s->priv_data;
  58. struct ogg_state *ost =
  59. av_malloc(sizeof(*ost) + (ogg->nstreams - 1) * sizeof(*ogg->streams));
  60. int i;
  61. ost->pos = avio_tell(s->pb);
  62. ost->curidx = ogg->curidx;
  63. ost->next = ogg->state;
  64. ost->nstreams = ogg->nstreams;
  65. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  66. for (i = 0; i < ogg->nstreams; i++) {
  67. struct ogg_stream *os = ogg->streams + i;
  68. os->buf = av_mallocz(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  69. memcpy(os->buf, ost->streams[i].buf, os->bufpos);
  70. }
  71. ogg->state = ost;
  72. return 0;
  73. }
  74. static int ogg_restore(AVFormatContext *s, int discard)
  75. {
  76. struct ogg *ogg = s->priv_data;
  77. AVIOContext *bc = s->pb;
  78. struct ogg_state *ost = ogg->state;
  79. int i, err;
  80. if (!ost)
  81. return 0;
  82. ogg->state = ost->next;
  83. if (!discard) {
  84. for (i = 0; i < ogg->nstreams; i++)
  85. av_freep(&ogg->streams[i].buf);
  86. avio_seek(bc, ost->pos, SEEK_SET);
  87. ogg->page_pos = -1;
  88. ogg->curidx = ost->curidx;
  89. ogg->nstreams = ost->nstreams;
  90. if ((err = av_reallocp_array(&ogg->streams, ogg->nstreams,
  91. sizeof(*ogg->streams))) < 0) {
  92. ogg->nstreams = 0;
  93. return err;
  94. } else
  95. memcpy(ogg->streams, ost->streams,
  96. ost->nstreams * sizeof(*ogg->streams));
  97. }
  98. av_free(ost);
  99. return 0;
  100. }
  101. static int ogg_reset(AVFormatContext *s)
  102. {
  103. struct ogg *ogg = s->priv_data;
  104. int i;
  105. int64_t start_pos = avio_tell(s->pb);
  106. for (i = 0; i < ogg->nstreams; i++) {
  107. struct ogg_stream *os = ogg->streams + i;
  108. os->bufpos = 0;
  109. os->pstart = 0;
  110. os->psize = 0;
  111. os->granule = -1;
  112. os->lastpts = AV_NOPTS_VALUE;
  113. os->lastdts = AV_NOPTS_VALUE;
  114. os->sync_pos = -1;
  115. os->page_pos = 0;
  116. os->nsegs = 0;
  117. os->segp = 0;
  118. os->incomplete = 0;
  119. os->got_data = 0;
  120. if (start_pos <= s->data_offset) {
  121. os->lastpts = 0;
  122. }
  123. }
  124. ogg->page_pos = -1;
  125. ogg->curidx = -1;
  126. return 0;
  127. }
  128. static const struct ogg_codec *ogg_find_codec(uint8_t *buf, int size)
  129. {
  130. int i;
  131. for (i = 0; ogg_codecs[i]; i++)
  132. if (size >= ogg_codecs[i]->magicsize &&
  133. !memcmp(buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  134. return ogg_codecs[i];
  135. return NULL;
  136. }
  137. /**
  138. * Replace the current stream with a new one. This is a typical webradio
  139. * situation where a new audio stream spawn (identified with a new serial) and
  140. * must replace the previous one (track switch).
  141. */
  142. static int ogg_replace_stream(AVFormatContext *s, uint32_t serial, int nsegs)
  143. {
  144. struct ogg *ogg = s->priv_data;
  145. struct ogg_stream *os;
  146. const struct ogg_codec *codec;
  147. int i = 0;
  148. if (s->pb->seekable) {
  149. uint8_t magic[8];
  150. int64_t pos = avio_tell(s->pb);
  151. avio_skip(s->pb, nsegs);
  152. avio_read(s->pb, magic, sizeof(magic));
  153. avio_seek(s->pb, pos, SEEK_SET);
  154. codec = ogg_find_codec(magic, sizeof(magic));
  155. if (!codec) {
  156. av_log(s, AV_LOG_ERROR, "Cannot identify new stream\n");
  157. return AVERROR_INVALIDDATA;
  158. }
  159. for (i = 0; i < ogg->nstreams; i++) {
  160. if (ogg->streams[i].codec == codec)
  161. break;
  162. }
  163. if (i >= ogg->nstreams)
  164. return ogg_new_stream(s, serial);
  165. } else if (ogg->nstreams != 1) {
  166. avpriv_report_missing_feature(s, "Changing stream parameters in multistream ogg");
  167. return AVERROR_PATCHWELCOME;
  168. }
  169. os = &ogg->streams[i];
  170. os->serial = serial;
  171. return i;
  172. #if 0
  173. buf = os->buf;
  174. bufsize = os->bufsize;
  175. codec = os->codec;
  176. if (!ogg->state || ogg->state->streams[i].private != os->private)
  177. av_freep(&ogg->streams[i].private);
  178. /* Set Ogg stream settings similar to what is done in ogg_new_stream(). We
  179. * also re-use the ogg_stream allocated buffer */
  180. memset(os, 0, sizeof(*os));
  181. os->serial = serial;
  182. os->bufsize = bufsize;
  183. os->buf = buf;
  184. os->header = -1;
  185. os->codec = codec;
  186. return i;
  187. #endif
  188. }
  189. static int ogg_new_stream(AVFormatContext *s, uint32_t serial)
  190. {
  191. struct ogg *ogg = s->priv_data;
  192. int idx = ogg->nstreams;
  193. AVStream *st;
  194. struct ogg_stream *os;
  195. size_t size;
  196. if (ogg->state) {
  197. av_log(s, AV_LOG_ERROR, "New streams are not supposed to be added "
  198. "in between Ogg context save/restore operations.\n");
  199. return AVERROR_BUG;
  200. }
  201. /* Allocate and init a new Ogg Stream */
  202. if (av_size_mult(ogg->nstreams + 1, sizeof(*ogg->streams), &size) < 0 ||
  203. !(os = av_realloc(ogg->streams, size)))
  204. return AVERROR(ENOMEM);
  205. ogg->streams = os;
  206. os = ogg->streams + idx;
  207. memset(os, 0, sizeof(*os));
  208. os->serial = serial;
  209. os->bufsize = DECODER_BUFFER_SIZE;
  210. os->buf = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  211. os->header = -1;
  212. os->start_granule = OGG_NOGRANULE_VALUE;
  213. if (!os->buf)
  214. return AVERROR(ENOMEM);
  215. /* Create the associated AVStream */
  216. st = avformat_new_stream(s, NULL);
  217. if (!st) {
  218. av_freep(&os->buf);
  219. return AVERROR(ENOMEM);
  220. }
  221. st->id = idx;
  222. avpriv_set_pts_info(st, 64, 1, 1000000);
  223. ogg->nstreams++;
  224. return idx;
  225. }
  226. static int ogg_new_buf(struct ogg *ogg, int idx)
  227. {
  228. struct ogg_stream *os = ogg->streams + idx;
  229. uint8_t *nb = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  230. int size = os->bufpos - os->pstart;
  231. if (os->buf) {
  232. memcpy(nb, os->buf + os->pstart, size);
  233. av_free(os->buf);
  234. }
  235. os->buf = nb;
  236. os->bufpos = size;
  237. os->pstart = 0;
  238. return 0;
  239. }
  240. static int data_packets_seen(const struct ogg *ogg)
  241. {
  242. int i;
  243. for (i = 0; i < ogg->nstreams; i++)
  244. if (ogg->streams[i].got_data)
  245. return 1;
  246. return 0;
  247. }
  248. static int ogg_read_page(AVFormatContext *s, int *sid)
  249. {
  250. AVIOContext *bc = s->pb;
  251. struct ogg *ogg = s->priv_data;
  252. struct ogg_stream *os;
  253. int ret, i = 0;
  254. int flags, nsegs;
  255. uint64_t gp;
  256. uint32_t serial;
  257. int size, idx;
  258. uint8_t sync[4];
  259. int sp = 0;
  260. ret = avio_read(bc, sync, 4);
  261. if (ret < 4)
  262. return ret < 0 ? ret : AVERROR_EOF;
  263. do {
  264. int c;
  265. if (sync[sp & 3] == 'O' &&
  266. sync[(sp + 1) & 3] == 'g' &&
  267. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  268. break;
  269. if(!i && bc->seekable && ogg->page_pos > 0) {
  270. memset(sync, 0, 4);
  271. avio_seek(bc, ogg->page_pos+4, SEEK_SET);
  272. ogg->page_pos = -1;
  273. }
  274. c = avio_r8(bc);
  275. if (url_feof(bc))
  276. return AVERROR_EOF;
  277. sync[sp++ & 3] = c;
  278. } while (i++ < MAX_PAGE_SIZE);
  279. if (i >= MAX_PAGE_SIZE) {
  280. av_log(s, AV_LOG_INFO, "cannot find sync word\n");
  281. return AVERROR_INVALIDDATA;
  282. }
  283. if (avio_r8(bc) != 0) { /* version */
  284. av_log (s, AV_LOG_ERROR, "ogg page, unsupported version\n");
  285. return AVERROR_INVALIDDATA;
  286. }
  287. flags = avio_r8(bc);
  288. gp = avio_rl64(bc);
  289. serial = avio_rl32(bc);
  290. avio_skip(bc, 8); /* seq, crc */
  291. nsegs = avio_r8(bc);
  292. idx = ogg_find_stream(ogg, serial);
  293. if (idx < 0) {
  294. if (data_packets_seen(ogg))
  295. idx = ogg_replace_stream(s, serial, nsegs);
  296. else
  297. idx = ogg_new_stream(s, serial);
  298. if (idx < 0) {
  299. av_log(s, AV_LOG_ERROR, "failed to create or replace stream\n");
  300. return idx;
  301. }
  302. }
  303. os = ogg->streams + idx;
  304. ogg->page_pos =
  305. os->page_pos = avio_tell(bc) - 27;
  306. if (os->psize > 0)
  307. ogg_new_buf(ogg, idx);
  308. ret = avio_read(bc, os->segments, nsegs);
  309. if (ret < nsegs)
  310. return ret < 0 ? ret : AVERROR_EOF;
  311. os->nsegs = nsegs;
  312. os->segp = 0;
  313. size = 0;
  314. for (i = 0; i < nsegs; i++)
  315. size += os->segments[i];
  316. if (!(flags & OGG_FLAG_BOS))
  317. os->got_data = 1;
  318. if (flags & OGG_FLAG_CONT || os->incomplete) {
  319. if (!os->psize) {
  320. // If this is the very first segment we started
  321. // playback in the middle of a continuation packet.
  322. // Discard it since we missed the start of it.
  323. while (os->segp < os->nsegs) {
  324. int seg = os->segments[os->segp++];
  325. os->pstart += seg;
  326. if (seg < 255)
  327. break;
  328. }
  329. os->sync_pos = os->page_pos;
  330. }
  331. } else {
  332. os->psize = 0;
  333. os->sync_pos = os->page_pos;
  334. }
  335. if (os->bufsize - os->bufpos < size) {
  336. uint8_t *nb = av_malloc((os->bufsize *= 2) + FF_INPUT_BUFFER_PADDING_SIZE);
  337. if (!nb)
  338. return AVERROR(ENOMEM);
  339. memcpy(nb, os->buf, os->bufpos);
  340. av_free(os->buf);
  341. os->buf = nb;
  342. }
  343. ret = avio_read(bc, os->buf + os->bufpos, size);
  344. if (ret < size)
  345. return ret < 0 ? ret : AVERROR_EOF;
  346. os->bufpos += size;
  347. os->granule = gp;
  348. os->flags = flags;
  349. memset(os->buf + os->bufpos, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  350. if (sid)
  351. *sid = idx;
  352. return 0;
  353. }
  354. /**
  355. * @brief find the next Ogg packet
  356. * @param *sid is set to the stream for the packet or -1 if there is
  357. * no matching stream, in that case assume all other return
  358. * values to be uninitialized.
  359. * @return negative value on error or EOF.
  360. */
  361. static int ogg_packet(AVFormatContext *s, int *sid, int *dstart, int *dsize,
  362. int64_t *fpos)
  363. {
  364. struct ogg *ogg = s->priv_data;
  365. int idx, i, ret;
  366. struct ogg_stream *os;
  367. int complete = 0;
  368. int segp = 0, psize = 0;
  369. av_dlog(s, "ogg_packet: curidx=%i\n", ogg->curidx);
  370. if (sid)
  371. *sid = -1;
  372. do {
  373. idx = ogg->curidx;
  374. while (idx < 0) {
  375. ret = ogg_read_page(s, &idx);
  376. if (ret < 0)
  377. return ret;
  378. }
  379. os = ogg->streams + idx;
  380. av_dlog(s, "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  381. idx, os->pstart, os->psize, os->segp, os->nsegs);
  382. if (!os->codec) {
  383. if (os->header < 0) {
  384. os->codec = ogg_find_codec(os->buf, os->bufpos);
  385. if (!os->codec) {
  386. av_log(s, AV_LOG_WARNING, "Codec not found\n");
  387. os->header = 0;
  388. return 0;
  389. }
  390. } else {
  391. return 0;
  392. }
  393. }
  394. segp = os->segp;
  395. psize = os->psize;
  396. while (os->segp < os->nsegs) {
  397. int ss = os->segments[os->segp++];
  398. os->psize += ss;
  399. if (ss < 255) {
  400. complete = 1;
  401. break;
  402. }
  403. }
  404. if (!complete && os->segp == os->nsegs) {
  405. ogg->curidx = -1;
  406. // Do not set incomplete for empty packets.
  407. // Together with the code in ogg_read_page
  408. // that discards all continuation of empty packets
  409. // we would get an infinite loop.
  410. os->incomplete = !!os->psize;
  411. }
  412. } while (!complete);
  413. if (os->granule == -1)
  414. av_log(s, AV_LOG_WARNING,
  415. "Page at %"PRId64" is missing granule\n",
  416. os->page_pos);
  417. ogg->curidx = idx;
  418. os->incomplete = 0;
  419. if (os->header) {
  420. os->header = os->codec->header(s, idx);
  421. if (!os->header) {
  422. os->segp = segp;
  423. os->psize = psize;
  424. // We have reached the first non-header packet in this stream.
  425. // Unfortunately more header packets may still follow for others,
  426. // but if we continue with header parsing we may lose data packets.
  427. ogg->headers = 1;
  428. // Update the header state for all streams and
  429. // compute the data_offset.
  430. if (!s->data_offset)
  431. s->data_offset = os->sync_pos;
  432. for (i = 0; i < ogg->nstreams; i++) {
  433. struct ogg_stream *cur_os = ogg->streams + i;
  434. // if we have a partial non-header packet, its start is
  435. // obviously at or after the data start
  436. if (cur_os->incomplete)
  437. s->data_offset = FFMIN(s->data_offset, cur_os->sync_pos);
  438. }
  439. } else {
  440. os->nb_header++;
  441. os->pstart += os->psize;
  442. os->psize = 0;
  443. }
  444. } else {
  445. os->pflags = 0;
  446. os->pduration = 0;
  447. if (os->codec && os->codec->packet)
  448. os->codec->packet(s, idx);
  449. if (sid)
  450. *sid = idx;
  451. if (dstart)
  452. *dstart = os->pstart;
  453. if (dsize)
  454. *dsize = os->psize;
  455. if (fpos)
  456. *fpos = os->sync_pos;
  457. os->pstart += os->psize;
  458. os->psize = 0;
  459. if(os->pstart == os->bufpos)
  460. os->bufpos = os->pstart = 0;
  461. os->sync_pos = os->page_pos;
  462. }
  463. // determine whether there are more complete packets in this page
  464. // if not, the page's granule will apply to this packet
  465. os->page_end = 1;
  466. for (i = os->segp; i < os->nsegs; i++)
  467. if (os->segments[i] < 255) {
  468. os->page_end = 0;
  469. break;
  470. }
  471. if (os->segp == os->nsegs)
  472. ogg->curidx = -1;
  473. return 0;
  474. }
  475. static int ogg_get_length(AVFormatContext *s)
  476. {
  477. struct ogg *ogg = s->priv_data;
  478. int i;
  479. int64_t size, end;
  480. int streams_left=0;
  481. if (!s->pb->seekable)
  482. return 0;
  483. // already set
  484. if (s->duration != AV_NOPTS_VALUE)
  485. return 0;
  486. size = avio_size(s->pb);
  487. if (size < 0)
  488. return 0;
  489. end = size > MAX_PAGE_SIZE ? size - MAX_PAGE_SIZE : 0;
  490. ogg_save(s);
  491. avio_seek(s->pb, end, SEEK_SET);
  492. ogg->page_pos = -1;
  493. while (!ogg_read_page(s, &i)) {
  494. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  495. ogg->streams[i].codec) {
  496. s->streams[i]->duration =
  497. ogg_gptopts(s, i, ogg->streams[i].granule, NULL);
  498. if (s->streams[i]->start_time != AV_NOPTS_VALUE) {
  499. s->streams[i]->duration -= s->streams[i]->start_time;
  500. streams_left-= (ogg->streams[i].got_start==-1);
  501. ogg->streams[i].got_start= 1;
  502. } else if(!ogg->streams[i].got_start) {
  503. ogg->streams[i].got_start= -1;
  504. streams_left++;
  505. }
  506. }
  507. }
  508. ogg_restore(s, 0);
  509. ogg_save (s);
  510. avio_seek (s->pb, s->data_offset, SEEK_SET);
  511. ogg_reset(s);
  512. while (streams_left > 0 && !ogg_packet(s, &i, NULL, NULL, NULL)) {
  513. int64_t pts;
  514. if (i < 0) continue;
  515. pts = ogg_calc_pts(s, i, NULL);
  516. if (pts != AV_NOPTS_VALUE && s->streams[i]->start_time == AV_NOPTS_VALUE && !ogg->streams[i].got_start) {
  517. s->streams[i]->duration -= pts;
  518. ogg->streams[i].got_start= 1;
  519. streams_left--;
  520. }else if(s->streams[i]->start_time != AV_NOPTS_VALUE && !ogg->streams[i].got_start) {
  521. ogg->streams[i].got_start= 1;
  522. streams_left--;
  523. }
  524. }
  525. ogg_restore (s, 0);
  526. return 0;
  527. }
  528. static int ogg_read_close(AVFormatContext *s)
  529. {
  530. struct ogg *ogg = s->priv_data;
  531. int i;
  532. for (i = 0; i < ogg->nstreams; i++) {
  533. av_freep(&ogg->streams[i].buf);
  534. if (ogg->streams[i].codec &&
  535. ogg->streams[i].codec->cleanup) {
  536. ogg->streams[i].codec->cleanup(s, i);
  537. }
  538. av_freep(&ogg->streams[i].private);
  539. }
  540. av_freep(&ogg->streams);
  541. return 0;
  542. }
  543. static int ogg_read_header(AVFormatContext *s)
  544. {
  545. struct ogg *ogg = s->priv_data;
  546. int ret, i;
  547. ogg->curidx = -1;
  548. //linear headers seek from start
  549. do {
  550. ret = ogg_packet(s, NULL, NULL, NULL, NULL);
  551. if (ret < 0) {
  552. ogg_read_close(s);
  553. return ret;
  554. }
  555. } while (!ogg->headers);
  556. av_dlog(s, "found headers\n");
  557. for (i = 0; i < ogg->nstreams; i++) {
  558. struct ogg_stream *os = ogg->streams + i;
  559. if (ogg->streams[i].header < 0) {
  560. av_log(s, AV_LOG_ERROR, "Header parsing failed for stream %d\n", i);
  561. ogg->streams[i].codec = NULL;
  562. } else if (os->codec && os->nb_header < os->codec->nb_header) {
  563. av_log(s, AV_LOG_WARNING,
  564. "Headers mismatch for stream %d: "
  565. "expected %d received %d.\n",
  566. i, os->codec->nb_header, os->nb_header);
  567. if (s->error_recognition & AV_EF_EXPLODE)
  568. return AVERROR_INVALIDDATA;
  569. }
  570. if (os->start_granule != OGG_NOGRANULE_VALUE)
  571. os->lastpts = s->streams[i]->start_time =
  572. ogg_gptopts(s, i, os->start_granule, NULL);
  573. }
  574. //linear granulepos seek from end
  575. ogg_get_length(s);
  576. return 0;
  577. }
  578. static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
  579. {
  580. struct ogg *ogg = s->priv_data;
  581. struct ogg_stream *os = ogg->streams + idx;
  582. int64_t pts = AV_NOPTS_VALUE;
  583. if (dts)
  584. *dts = AV_NOPTS_VALUE;
  585. if (os->lastpts != AV_NOPTS_VALUE) {
  586. pts = os->lastpts;
  587. os->lastpts = AV_NOPTS_VALUE;
  588. }
  589. if (os->lastdts != AV_NOPTS_VALUE) {
  590. if (dts)
  591. *dts = os->lastdts;
  592. os->lastdts = AV_NOPTS_VALUE;
  593. }
  594. if (os->page_end) {
  595. if (os->granule != -1LL) {
  596. if (os->codec && os->codec->granule_is_start)
  597. pts = ogg_gptopts(s, idx, os->granule, dts);
  598. else
  599. os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
  600. os->granule = -1LL;
  601. }
  602. }
  603. return pts;
  604. }
  605. static void ogg_validate_keyframe(AVFormatContext *s, int idx, int pstart, int psize)
  606. {
  607. struct ogg *ogg = s->priv_data;
  608. struct ogg_stream *os = ogg->streams + idx;
  609. if (psize && s->streams[idx]->codec->codec_id == AV_CODEC_ID_THEORA) {
  610. if (!!(os->pflags & AV_PKT_FLAG_KEY) != !(os->buf[pstart] & 0x40)) {
  611. os->pflags ^= AV_PKT_FLAG_KEY;
  612. av_log(s, AV_LOG_WARNING, "Broken file, %skeyframe not correctly marked.\n",
  613. (os->pflags & AV_PKT_FLAG_KEY) ? "" : "non-");
  614. }
  615. }
  616. }
  617. static int ogg_read_packet(AVFormatContext *s, AVPacket *pkt)
  618. {
  619. struct ogg *ogg;
  620. struct ogg_stream *os;
  621. int idx, ret;
  622. int pstart, psize;
  623. int64_t fpos, pts, dts;
  624. if (s->io_repositioned) {
  625. ogg_reset(s);
  626. s->io_repositioned = 0;
  627. }
  628. //Get an ogg packet
  629. retry:
  630. do {
  631. ret = ogg_packet(s, &idx, &pstart, &psize, &fpos);
  632. if (ret < 0)
  633. return ret;
  634. } while (idx < 0 || !s->streams[idx]);
  635. ogg = s->priv_data;
  636. os = ogg->streams + idx;
  637. // pflags might not be set until after this
  638. pts = ogg_calc_pts(s, idx, &dts);
  639. ogg_validate_keyframe(s, idx, pstart, psize);
  640. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  641. goto retry;
  642. os->keyframe_seek = 0;
  643. //Alloc a pkt
  644. ret = av_new_packet(pkt, psize);
  645. if (ret < 0)
  646. return ret;
  647. pkt->stream_index = idx;
  648. memcpy(pkt->data, os->buf + pstart, psize);
  649. pkt->pts = pts;
  650. pkt->dts = dts;
  651. pkt->flags = os->pflags;
  652. pkt->duration = os->pduration;
  653. pkt->pos = fpos;
  654. if (os->end_trimming) {
  655. uint8_t *side_data = av_packet_new_side_data(pkt,
  656. AV_PKT_DATA_SKIP_SAMPLES,
  657. 10);
  658. if(side_data == NULL) {
  659. av_free_packet(pkt);
  660. av_free(pkt);
  661. return AVERROR(ENOMEM);
  662. }
  663. AV_WL32(side_data + 4, os->end_trimming);
  664. }
  665. return psize;
  666. }
  667. static int64_t ogg_read_timestamp(AVFormatContext *s, int stream_index,
  668. int64_t *pos_arg, int64_t pos_limit)
  669. {
  670. struct ogg *ogg = s->priv_data;
  671. AVIOContext *bc = s->pb;
  672. int64_t pts = AV_NOPTS_VALUE;
  673. int64_t keypos = -1;
  674. int i;
  675. int pstart, psize;
  676. avio_seek(bc, *pos_arg, SEEK_SET);
  677. ogg_reset(s);
  678. while ( avio_tell(bc) <= pos_limit
  679. && !ogg_packet(s, &i, &pstart, &psize, pos_arg)) {
  680. if (i == stream_index) {
  681. struct ogg_stream *os = ogg->streams + stream_index;
  682. pts = ogg_calc_pts(s, i, NULL);
  683. ogg_validate_keyframe(s, i, pstart, psize);
  684. if (os->pflags & AV_PKT_FLAG_KEY) {
  685. keypos = *pos_arg;
  686. } else if (os->keyframe_seek) {
  687. // if we had a previous keyframe but no pts for it,
  688. // return that keyframe with this pts value.
  689. if (keypos >= 0)
  690. *pos_arg = keypos;
  691. else
  692. pts = AV_NOPTS_VALUE;
  693. }
  694. }
  695. if (pts != AV_NOPTS_VALUE)
  696. break;
  697. }
  698. ogg_reset(s);
  699. return pts;
  700. }
  701. static int ogg_read_seek(AVFormatContext *s, int stream_index,
  702. int64_t timestamp, int flags)
  703. {
  704. struct ogg *ogg = s->priv_data;
  705. struct ogg_stream *os = ogg->streams + stream_index;
  706. int ret;
  707. av_assert0(stream_index < ogg->nstreams);
  708. // Ensure everything is reset even when seeking via
  709. // the generated index.
  710. ogg_reset(s);
  711. // Try seeking to a keyframe first. If this fails (very possible),
  712. // av_seek_frame will fall back to ignoring keyframes
  713. if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO
  714. && !(flags & AVSEEK_FLAG_ANY))
  715. os->keyframe_seek = 1;
  716. ret = ff_seek_frame_binary(s, stream_index, timestamp, flags);
  717. os = ogg->streams + stream_index;
  718. if (ret < 0)
  719. os->keyframe_seek = 0;
  720. return ret;
  721. }
  722. static int ogg_probe(AVProbeData *p)
  723. {
  724. if (!memcmp("OggS", p->buf, 5) && p->buf[5] <= 0x7)
  725. return AVPROBE_SCORE_MAX;
  726. return 0;
  727. }
  728. AVInputFormat ff_ogg_demuxer = {
  729. .name = "ogg",
  730. .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
  731. .priv_data_size = sizeof(struct ogg),
  732. .read_probe = ogg_probe,
  733. .read_header = ogg_read_header,
  734. .read_packet = ogg_read_packet,
  735. .read_close = ogg_read_close,
  736. .read_seek = ogg_read_seek,
  737. .read_timestamp = ogg_read_timestamp,
  738. .extensions = "ogg",
  739. .flags = AVFMT_GENERIC_INDEX | AVFMT_TS_DISCONT | AVFMT_NOBINSEARCH,
  740. };