oggdec.c 22 KB

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