oggdec.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 "oggdec.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #include "vorbiscomment.h"
  31. #define MAX_PAGE_SIZE 65307
  32. #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
  33. static const struct ogg_codec * const ogg_codecs[] = {
  34. &ff_skeleton_codec,
  35. &ff_dirac_codec,
  36. &ff_speex_codec,
  37. &ff_vorbis_codec,
  38. &ff_theora_codec,
  39. &ff_flac_codec,
  40. &ff_celt_codec,
  41. &ff_old_dirac_codec,
  42. &ff_old_flac_codec,
  43. &ff_ogm_video_codec,
  44. &ff_ogm_audio_codec,
  45. &ff_ogm_text_codec,
  46. &ff_ogm_old_codec,
  47. NULL
  48. };
  49. //FIXME We could avoid some structure duplication
  50. static int ogg_save(AVFormatContext *s)
  51. {
  52. struct ogg *ogg = s->priv_data;
  53. struct ogg_state *ost =
  54. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  55. int i;
  56. ost->pos = avio_tell (s->pb);
  57. ost->curidx = ogg->curidx;
  58. ost->next = ogg->state;
  59. ost->nstreams = ogg->nstreams;
  60. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  61. for (i = 0; i < ogg->nstreams; i++){
  62. struct ogg_stream *os = ogg->streams + i;
  63. os->buf = av_mallocz (os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  64. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  65. }
  66. ogg->state = ost;
  67. return 0;
  68. }
  69. static int ogg_restore(AVFormatContext *s, int discard)
  70. {
  71. struct ogg *ogg = s->priv_data;
  72. AVIOContext *bc = s->pb;
  73. struct ogg_state *ost = ogg->state;
  74. int i;
  75. if (!ost)
  76. return 0;
  77. ogg->state = ost->next;
  78. if (!discard){
  79. struct ogg_stream *old_streams = ogg->streams;
  80. for (i = 0; i < ogg->nstreams; i++)
  81. av_free (ogg->streams[i].buf);
  82. avio_seek (bc, ost->pos, SEEK_SET);
  83. ogg->curidx = ost->curidx;
  84. ogg->nstreams = ost->nstreams;
  85. ogg->streams = av_realloc (ogg->streams,
  86. ogg->nstreams * sizeof (*ogg->streams));
  87. if (ogg->streams) {
  88. memcpy(ogg->streams, ost->streams,
  89. ost->nstreams * sizeof(*ogg->streams));
  90. } else {
  91. av_free(old_streams);
  92. ogg->nstreams = 0;
  93. }
  94. }
  95. av_free (ost);
  96. return 0;
  97. }
  98. static int ogg_reset(struct ogg *ogg)
  99. {
  100. int i;
  101. for (i = 0; i < ogg->nstreams; i++){
  102. struct ogg_stream *os = ogg->streams + i;
  103. os->bufpos = 0;
  104. os->pstart = 0;
  105. os->psize = 0;
  106. os->granule = -1;
  107. os->lastpts = AV_NOPTS_VALUE;
  108. os->lastdts = AV_NOPTS_VALUE;
  109. os->sync_pos = -1;
  110. os->page_pos = 0;
  111. os->nsegs = 0;
  112. os->segp = 0;
  113. os->incomplete = 0;
  114. }
  115. ogg->curidx = -1;
  116. return 0;
  117. }
  118. static const struct ogg_codec *ogg_find_codec(uint8_t *buf, int size)
  119. {
  120. int i;
  121. for (i = 0; ogg_codecs[i]; i++)
  122. if (size >= ogg_codecs[i]->magicsize &&
  123. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  124. return ogg_codecs[i];
  125. return NULL;
  126. }
  127. static int ogg_new_stream(AVFormatContext *s, uint32_t serial, int new_avstream)
  128. {
  129. struct ogg *ogg = s->priv_data;
  130. int idx = ogg->nstreams++;
  131. AVStream *st;
  132. struct ogg_stream *os;
  133. os = av_realloc (ogg->streams, ogg->nstreams * sizeof (*ogg->streams));
  134. if (!os)
  135. return AVERROR(ENOMEM);
  136. ogg->streams = os;
  137. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  138. os = ogg->streams + idx;
  139. os->serial = serial;
  140. os->bufsize = DECODER_BUFFER_SIZE;
  141. os->buf = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  142. os->header = -1;
  143. if (new_avstream) {
  144. st = avformat_new_stream(s, NULL);
  145. if (!st)
  146. return AVERROR(ENOMEM);
  147. st->id = idx;
  148. avpriv_set_pts_info(st, 64, 1, 1000000);
  149. }
  150. return idx;
  151. }
  152. static int ogg_new_buf(struct ogg *ogg, int idx)
  153. {
  154. struct ogg_stream *os = ogg->streams + idx;
  155. uint8_t *nb = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  156. int size = os->bufpos - os->pstart;
  157. if(os->buf){
  158. memcpy(nb, os->buf + os->pstart, size);
  159. av_free(os->buf);
  160. }
  161. os->buf = nb;
  162. os->bufpos = size;
  163. os->pstart = 0;
  164. return 0;
  165. }
  166. static int ogg_read_page(AVFormatContext *s, int *str)
  167. {
  168. AVIOContext *bc = s->pb;
  169. struct ogg *ogg = s->priv_data;
  170. struct ogg_stream *os;
  171. int ret, i = 0;
  172. int flags, nsegs;
  173. uint64_t gp;
  174. uint32_t serial;
  175. int size, idx;
  176. uint8_t sync[4];
  177. int sp = 0;
  178. ret = avio_read(bc, sync, 4);
  179. if (ret < 4)
  180. return ret < 0 ? ret : AVERROR_EOF;
  181. do{
  182. int c;
  183. if (sync[sp & 3] == 'O' &&
  184. sync[(sp + 1) & 3] == 'g' &&
  185. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  186. break;
  187. c = avio_r8(bc);
  188. if (url_feof(bc))
  189. return AVERROR_EOF;
  190. sync[sp++ & 3] = c;
  191. }while (i++ < MAX_PAGE_SIZE);
  192. if (i >= MAX_PAGE_SIZE){
  193. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  194. return AVERROR_INVALIDDATA;
  195. }
  196. if (avio_r8(bc) != 0) /* version */
  197. return AVERROR_INVALIDDATA;
  198. flags = avio_r8(bc);
  199. gp = avio_rl64 (bc);
  200. serial = avio_rl32 (bc);
  201. avio_skip(bc, 8); /* seq, crc */
  202. nsegs = avio_r8(bc);
  203. idx = ogg_find_stream (ogg, serial);
  204. if (idx < 0){
  205. if (ogg->headers) {
  206. int n;
  207. for (n = 0; n < ogg->nstreams; n++) {
  208. av_freep(&ogg->streams[n].buf);
  209. if (!ogg->state || ogg->state->streams[n].private != ogg->streams[n].private)
  210. av_freep(&ogg->streams[n].private);
  211. }
  212. ogg->curidx = -1;
  213. ogg->nstreams = 0;
  214. idx = ogg_new_stream(s, serial, 0);
  215. } else {
  216. idx = ogg_new_stream(s, serial, 1);
  217. }
  218. if (idx < 0)
  219. return idx;
  220. }
  221. os = ogg->streams + idx;
  222. os->page_pos = avio_tell(bc) - 27;
  223. if(os->psize > 0)
  224. ogg_new_buf(ogg, idx);
  225. ret = avio_read(bc, os->segments, nsegs);
  226. if (ret < nsegs)
  227. return ret < 0 ? ret : AVERROR_EOF;
  228. os->nsegs = nsegs;
  229. os->segp = 0;
  230. size = 0;
  231. for (i = 0; i < nsegs; i++)
  232. size += os->segments[i];
  233. if (flags & OGG_FLAG_CONT || os->incomplete){
  234. if (!os->psize){
  235. while (os->segp < os->nsegs){
  236. int seg = os->segments[os->segp++];
  237. os->pstart += seg;
  238. if (seg < 255)
  239. break;
  240. }
  241. os->sync_pos = os->page_pos;
  242. }
  243. }else{
  244. os->psize = 0;
  245. os->sync_pos = os->page_pos;
  246. }
  247. if (os->bufsize - os->bufpos < size){
  248. uint8_t *nb = av_malloc ((os->bufsize *= 2) + FF_INPUT_BUFFER_PADDING_SIZE);
  249. if (!nb)
  250. return AVERROR(ENOMEM);
  251. memcpy (nb, os->buf, os->bufpos);
  252. av_free (os->buf);
  253. os->buf = nb;
  254. }
  255. ret = avio_read(bc, os->buf + os->bufpos, size);
  256. if (ret < size)
  257. return ret < 0 ? ret : AVERROR_EOF;
  258. os->bufpos += size;
  259. os->granule = gp;
  260. os->flags = flags;
  261. memset(os->buf + os->bufpos, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  262. if (str)
  263. *str = idx;
  264. return 0;
  265. }
  266. static int ogg_packet(AVFormatContext *s, int *str, int *dstart, int *dsize,
  267. int64_t *fpos)
  268. {
  269. struct ogg *ogg = s->priv_data;
  270. int idx, i, ret;
  271. struct ogg_stream *os;
  272. int complete = 0;
  273. int segp = 0, psize = 0;
  274. av_dlog(s, "ogg_packet: curidx=%i\n", ogg->curidx);
  275. do{
  276. idx = ogg->curidx;
  277. while (idx < 0){
  278. ret = ogg_read_page(s, &idx);
  279. if (ret < 0)
  280. return ret;
  281. }
  282. os = ogg->streams + idx;
  283. av_dlog(s, "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  284. idx, os->pstart, os->psize, os->segp, os->nsegs);
  285. if (!os->codec){
  286. if (os->header < 0){
  287. os->codec = ogg_find_codec (os->buf, os->bufpos);
  288. if (!os->codec){
  289. av_log(s, AV_LOG_WARNING, "Codec not found\n");
  290. os->header = 0;
  291. return 0;
  292. }
  293. }else{
  294. return 0;
  295. }
  296. }
  297. segp = os->segp;
  298. psize = os->psize;
  299. while (os->segp < os->nsegs){
  300. int ss = os->segments[os->segp++];
  301. os->psize += ss;
  302. if (ss < 255){
  303. complete = 1;
  304. break;
  305. }
  306. }
  307. if (!complete && os->segp == os->nsegs){
  308. ogg->curidx = -1;
  309. // Do not set incomplete for empty packets.
  310. // Together with the code in ogg_read_page
  311. // that discards all continuation of empty packets
  312. // we would get an infinite loop.
  313. os->incomplete = !!os->psize;
  314. }
  315. }while (!complete);
  316. if (os->granule == -1)
  317. av_log(s, AV_LOG_WARNING, "Page at %"PRId64" is missing granule\n", os->page_pos);
  318. ogg->curidx = idx;
  319. os->incomplete = 0;
  320. if (os->header) {
  321. os->header = os->codec->header (s, idx);
  322. if (!os->header){
  323. os->segp = segp;
  324. os->psize = psize;
  325. // We have reached the first non-header packet in this stream.
  326. // Unfortunately more header packets may still follow for others,
  327. // but if we continue with header parsing we may lose data packets.
  328. ogg->headers = 1;
  329. // Update the header state for all streams and
  330. // compute the data_offset.
  331. if (!s->data_offset)
  332. s->data_offset = os->sync_pos;
  333. for (i = 0; i < ogg->nstreams; i++) {
  334. struct ogg_stream *cur_os = ogg->streams + i;
  335. // if we have a partial non-header packet, its start is
  336. // obviously at or after the data start
  337. if (cur_os->incomplete)
  338. s->data_offset = FFMIN(s->data_offset, cur_os->sync_pos);
  339. }
  340. }else{
  341. os->pstart += os->psize;
  342. os->psize = 0;
  343. }
  344. } else {
  345. os->pflags = 0;
  346. os->pduration = 0;
  347. if (os->codec && os->codec->packet)
  348. os->codec->packet (s, idx);
  349. if (str)
  350. *str = idx;
  351. if (dstart)
  352. *dstart = os->pstart;
  353. if (dsize)
  354. *dsize = os->psize;
  355. if (fpos)
  356. *fpos = os->sync_pos;
  357. os->pstart += os->psize;
  358. os->psize = 0;
  359. if(os->pstart == os->bufpos)
  360. os->bufpos = os->pstart = 0;
  361. os->sync_pos = os->page_pos;
  362. }
  363. // determine whether there are more complete packets in this page
  364. // if not, the page's granule will apply to this packet
  365. os->page_end = 1;
  366. for (i = os->segp; i < os->nsegs; i++)
  367. if (os->segments[i] < 255) {
  368. os->page_end = 0;
  369. break;
  370. }
  371. if (os->segp == os->nsegs)
  372. ogg->curidx = -1;
  373. return 0;
  374. }
  375. static int ogg_get_headers(AVFormatContext *s)
  376. {
  377. struct ogg *ogg = s->priv_data;
  378. int ret;
  379. do{
  380. ret = ogg_packet(s, NULL, NULL, NULL, NULL);
  381. if (ret < 0)
  382. return ret;
  383. }while (!ogg->headers);
  384. av_dlog(s, "found headers\n");
  385. return 0;
  386. }
  387. static int ogg_get_length(AVFormatContext *s)
  388. {
  389. struct ogg *ogg = s->priv_data;
  390. int i;
  391. int64_t size, end;
  392. int streams_left=0;
  393. if(!s->pb->seekable)
  394. return 0;
  395. // already set
  396. if (s->duration != AV_NOPTS_VALUE)
  397. return 0;
  398. size = avio_size(s->pb);
  399. if(size < 0)
  400. return 0;
  401. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
  402. ogg_save (s);
  403. avio_seek (s->pb, end, SEEK_SET);
  404. while (!ogg_read_page (s, &i)){
  405. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  406. ogg->streams[i].codec) {
  407. s->streams[i]->duration =
  408. ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
  409. if (s->streams[i]->start_time != AV_NOPTS_VALUE){
  410. s->streams[i]->duration -= s->streams[i]->start_time;
  411. streams_left-= (ogg->streams[i].got_start==-1);
  412. ogg->streams[i].got_start= 1;
  413. }else if(!ogg->streams[i].got_start){
  414. ogg->streams[i].got_start= -1;
  415. streams_left++;
  416. }
  417. }
  418. }
  419. ogg_restore (s, 0);
  420. ogg_save (s);
  421. avio_seek (s->pb, 0, SEEK_SET);
  422. while (!ogg_read_page (s, &i)){
  423. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  424. ogg->streams[i].codec) {
  425. if(s->streams[i]->duration && s->streams[i]->start_time == AV_NOPTS_VALUE && !ogg->streams[i].got_start){
  426. int64_t start= ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
  427. if(av_rescale_q(start, s->streams[i]->time_base, AV_TIME_BASE_Q) > AV_TIME_BASE)
  428. s->streams[i]->duration -= start;
  429. ogg->streams[i].got_start= 1;
  430. streams_left--;
  431. }
  432. if(streams_left<=0)
  433. break;
  434. }
  435. }
  436. ogg_restore (s, 0);
  437. return 0;
  438. }
  439. static int ogg_read_close(AVFormatContext *s)
  440. {
  441. struct ogg *ogg = s->priv_data;
  442. int i;
  443. for (i = 0; i < ogg->nstreams; i++) {
  444. av_free(ogg->streams[i].buf);
  445. if (ogg->streams[i].codec &&
  446. ogg->streams[i].codec->cleanup) {
  447. ogg->streams[i].codec->cleanup(s, i);
  448. }
  449. av_free(ogg->streams[i].private);
  450. }
  451. av_free(ogg->streams);
  452. return 0;
  453. }
  454. static int ogg_read_header(AVFormatContext *s)
  455. {
  456. struct ogg *ogg = s->priv_data;
  457. int ret, i;
  458. ogg->curidx = -1;
  459. //linear headers seek from start
  460. ret = ogg_get_headers(s);
  461. if (ret < 0) {
  462. ogg_read_close(s);
  463. return ret;
  464. }
  465. for (i = 0; i < ogg->nstreams; i++)
  466. if (ogg->streams[i].header < 0)
  467. ogg->streams[i].codec = NULL;
  468. //linear granulepos seek from end
  469. ogg_get_length (s);
  470. //fill the extradata in the per codec callbacks
  471. return 0;
  472. }
  473. static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
  474. {
  475. struct ogg *ogg = s->priv_data;
  476. struct ogg_stream *os = ogg->streams + idx;
  477. int64_t pts = AV_NOPTS_VALUE;
  478. if (dts)
  479. *dts = AV_NOPTS_VALUE;
  480. if (os->lastpts != AV_NOPTS_VALUE) {
  481. pts = os->lastpts;
  482. os->lastpts = AV_NOPTS_VALUE;
  483. }
  484. if (os->lastdts != AV_NOPTS_VALUE) {
  485. if (dts)
  486. *dts = os->lastdts;
  487. os->lastdts = AV_NOPTS_VALUE;
  488. }
  489. if (os->page_end) {
  490. if (os->granule != -1LL) {
  491. if (os->codec && os->codec->granule_is_start)
  492. pts = ogg_gptopts(s, idx, os->granule, dts);
  493. else
  494. os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
  495. os->granule = -1LL;
  496. }
  497. }
  498. return pts;
  499. }
  500. static int ogg_read_packet(AVFormatContext *s, AVPacket *pkt)
  501. {
  502. struct ogg *ogg;
  503. struct ogg_stream *os;
  504. int idx = -1, ret;
  505. int pstart, psize;
  506. int64_t fpos, pts, dts;
  507. //Get an ogg packet
  508. retry:
  509. do{
  510. ret = ogg_packet(s, &idx, &pstart, &psize, &fpos);
  511. if (ret < 0)
  512. return ret;
  513. }while (idx < 0 || !s->streams[idx]);
  514. ogg = s->priv_data;
  515. os = ogg->streams + idx;
  516. // pflags might not be set until after this
  517. pts = ogg_calc_pts(s, idx, &dts);
  518. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  519. goto retry;
  520. os->keyframe_seek = 0;
  521. //Alloc a pkt
  522. ret = av_new_packet(pkt, psize);
  523. if (ret < 0)
  524. return ret;
  525. pkt->stream_index = idx;
  526. memcpy (pkt->data, os->buf + pstart, psize);
  527. pkt->pts = pts;
  528. pkt->dts = dts;
  529. pkt->flags = os->pflags;
  530. pkt->duration = os->pduration;
  531. pkt->pos = fpos;
  532. return psize;
  533. }
  534. static int64_t ogg_read_timestamp(AVFormatContext *s, int stream_index,
  535. int64_t *pos_arg, int64_t pos_limit)
  536. {
  537. struct ogg *ogg = s->priv_data;
  538. AVIOContext *bc = s->pb;
  539. int64_t pts = AV_NOPTS_VALUE;
  540. int i = -1;
  541. avio_seek(bc, *pos_arg, SEEK_SET);
  542. ogg_reset(ogg);
  543. while (avio_tell(bc) < pos_limit && !ogg_packet(s, &i, NULL, NULL, pos_arg)) {
  544. if (i == stream_index) {
  545. struct ogg_stream *os = ogg->streams + stream_index;
  546. pts = ogg_calc_pts(s, i, NULL);
  547. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  548. pts = AV_NOPTS_VALUE;
  549. }
  550. if (pts != AV_NOPTS_VALUE)
  551. break;
  552. }
  553. ogg_reset(ogg);
  554. return pts;
  555. }
  556. static int ogg_read_seek(AVFormatContext *s, int stream_index,
  557. int64_t timestamp, int flags)
  558. {
  559. struct ogg *ogg = s->priv_data;
  560. struct ogg_stream *os = ogg->streams + stream_index;
  561. int ret;
  562. // Try seeking to a keyframe first. If this fails (very possible),
  563. // av_seek_frame will fall back to ignoring keyframes
  564. if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO
  565. && !(flags & AVSEEK_FLAG_ANY))
  566. os->keyframe_seek = 1;
  567. ret = ff_seek_frame_binary(s, stream_index, timestamp, flags);
  568. os = ogg->streams + stream_index;
  569. if (ret < 0)
  570. os->keyframe_seek = 0;
  571. return ret;
  572. }
  573. static int ogg_probe(AVProbeData *p)
  574. {
  575. if (!memcmp("OggS", p->buf, 5) && p->buf[5] <= 0x7)
  576. return AVPROBE_SCORE_MAX;
  577. return 0;
  578. }
  579. AVInputFormat ff_ogg_demuxer = {
  580. .name = "ogg",
  581. .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
  582. .priv_data_size = sizeof(struct ogg),
  583. .read_probe = ogg_probe,
  584. .read_header = ogg_read_header,
  585. .read_packet = ogg_read_packet,
  586. .read_close = ogg_read_close,
  587. .read_seek = ogg_read_seek,
  588. .read_timestamp = ogg_read_timestamp,
  589. .extensions = "ogg",
  590. .flags = AVFMT_GENERIC_INDEX,
  591. };