oggdec.c 17 KB

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