oggdec.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. #define MAX_PAGE_SIZE 65307
  31. #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
  32. static const struct ogg_codec * const ogg_codecs[] = {
  33. &ff_speex_codec,
  34. &ff_vorbis_codec,
  35. &ff_theora_codec,
  36. &ff_flac_codec,
  37. &ff_old_flac_codec,
  38. &ff_ogm_video_codec,
  39. &ff_ogm_audio_codec,
  40. &ff_ogm_text_codec,
  41. &ff_ogm_old_codec,
  42. NULL
  43. };
  44. //FIXME We could avoid some structure duplication
  45. static int
  46. ogg_save (AVFormatContext * s)
  47. {
  48. struct ogg *ogg = s->priv_data;
  49. struct ogg_state *ost =
  50. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  51. int i;
  52. ost->pos = url_ftell (s->pb);
  53. ost->curidx = ogg->curidx;
  54. ost->next = ogg->state;
  55. ost->nstreams = ogg->nstreams;
  56. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  57. for (i = 0; i < ogg->nstreams; i++){
  58. struct ogg_stream *os = ogg->streams + i;
  59. os->buf = av_mallocz (os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  60. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  61. }
  62. ogg->state = ost;
  63. return 0;
  64. }
  65. static int
  66. ogg_restore (AVFormatContext * s, int discard)
  67. {
  68. struct ogg *ogg = s->priv_data;
  69. ByteIOContext *bc = s->pb;
  70. struct ogg_state *ost = ogg->state;
  71. int i;
  72. if (!ost)
  73. return 0;
  74. ogg->state = ost->next;
  75. if (!discard){
  76. struct ogg_stream *old_streams = ogg->streams;
  77. for (i = 0; i < ogg->nstreams; i++)
  78. av_free (ogg->streams[i].buf);
  79. url_fseek (bc, ost->pos, SEEK_SET);
  80. ogg->curidx = ost->curidx;
  81. ogg->nstreams = ost->nstreams;
  82. ogg->streams = av_realloc (ogg->streams,
  83. ogg->nstreams * sizeof (*ogg->streams));
  84. if (ogg->streams) {
  85. memcpy(ogg->streams, ost->streams,
  86. ost->nstreams * sizeof(*ogg->streams));
  87. } else {
  88. av_free(old_streams);
  89. ogg->nstreams = 0;
  90. }
  91. }
  92. av_free (ost);
  93. return 0;
  94. }
  95. static int
  96. ogg_reset (struct ogg * ogg)
  97. {
  98. int i;
  99. for (i = 0; i < ogg->nstreams; i++){
  100. struct ogg_stream *os = ogg->streams + i;
  101. os->bufpos = 0;
  102. os->pstart = 0;
  103. os->psize = 0;
  104. os->granule = -1;
  105. os->lastgp = -1;
  106. os->nsegs = 0;
  107. os->segp = 0;
  108. }
  109. ogg->curidx = -1;
  110. return 0;
  111. }
  112. static const struct ogg_codec *
  113. ogg_find_codec (uint8_t * buf, int size)
  114. {
  115. int i;
  116. for (i = 0; ogg_codecs[i]; i++)
  117. if (size >= ogg_codecs[i]->magicsize &&
  118. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  119. return ogg_codecs[i];
  120. return NULL;
  121. }
  122. static int
  123. ogg_find_stream (struct ogg * ogg, int serial)
  124. {
  125. int i;
  126. for (i = 0; i < ogg->nstreams; i++)
  127. if (ogg->streams[i].serial == serial)
  128. return i;
  129. return -1;
  130. }
  131. static int
  132. ogg_new_stream (AVFormatContext * s, uint32_t serial)
  133. {
  134. struct ogg *ogg = s->priv_data;
  135. int idx = ogg->nstreams++;
  136. AVStream *st;
  137. struct ogg_stream *os;
  138. os = av_realloc (ogg->streams, ogg->nstreams * sizeof (*ogg->streams));
  139. if (!os)
  140. return AVERROR(ENOMEM);
  141. ogg->streams = os;
  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. st = av_new_stream (s, idx);
  149. if (!st)
  150. return AVERROR(ENOMEM);
  151. av_set_pts_info(st, 64, 1, 1000000);
  152. return idx;
  153. }
  154. static int
  155. ogg_new_buf(struct ogg *ogg, int idx)
  156. {
  157. struct ogg_stream *os = ogg->streams + idx;
  158. uint8_t *nb = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
  159. int size = os->bufpos - os->pstart;
  160. if(os->buf){
  161. memcpy(nb, os->buf + os->pstart, size);
  162. av_free(os->buf);
  163. }
  164. os->buf = nb;
  165. os->bufpos = size;
  166. os->pstart = 0;
  167. return 0;
  168. }
  169. static int
  170. ogg_read_page (AVFormatContext * s, int *str)
  171. {
  172. ByteIOContext *bc = s->pb;
  173. struct ogg *ogg = s->priv_data;
  174. struct ogg_stream *os;
  175. int i = 0;
  176. int flags, nsegs;
  177. uint64_t gp;
  178. uint32_t serial;
  179. uint32_t seq;
  180. uint32_t crc;
  181. int size, idx;
  182. uint8_t sync[4];
  183. int sp = 0;
  184. if (get_buffer (bc, sync, 4) < 4)
  185. return -1;
  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 = url_fgetc (bc);
  193. if (c < 0)
  194. return -1;
  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 -1;
  200. }
  201. if (url_fgetc (bc) != 0) /* version */
  202. return -1;
  203. flags = url_fgetc (bc);
  204. gp = get_le64 (bc);
  205. serial = get_le32 (bc);
  206. seq = get_le32 (bc);
  207. crc = get_le32 (bc);
  208. nsegs = url_fgetc (bc);
  209. idx = ogg_find_stream (ogg, serial);
  210. if (idx < 0){
  211. idx = ogg_new_stream (s, serial);
  212. if (idx < 0)
  213. return -1;
  214. }
  215. os = ogg->streams + idx;
  216. if(os->psize > 0)
  217. ogg_new_buf(ogg, idx);
  218. if (get_buffer (bc, os->segments, nsegs) < nsegs)
  219. return -1;
  220. os->nsegs = nsegs;
  221. os->segp = 0;
  222. size = 0;
  223. for (i = 0; i < nsegs; i++)
  224. size += os->segments[i];
  225. if (flags & OGG_FLAG_CONT){
  226. if (!os->psize){
  227. while (os->segp < os->nsegs){
  228. int seg = os->segments[os->segp++];
  229. os->pstart += seg;
  230. if (seg < 255)
  231. break;
  232. }
  233. }
  234. }else{
  235. os->psize = 0;
  236. }
  237. if (os->bufsize - os->bufpos < size){
  238. uint8_t *nb = av_malloc ((os->bufsize *= 2) + FF_INPUT_BUFFER_PADDING_SIZE);
  239. if (!nb)
  240. return AVERROR(ENOMEM);
  241. memcpy (nb, os->buf, os->bufpos);
  242. av_free (os->buf);
  243. os->buf = nb;
  244. }
  245. if (get_buffer (bc, os->buf + os->bufpos, size) < size)
  246. return -1;
  247. os->lastgp = os->granule;
  248. os->bufpos += size;
  249. os->granule = gp;
  250. os->flags = flags;
  251. memset(os->buf + os->bufpos, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  252. if (str)
  253. *str = idx;
  254. return 0;
  255. }
  256. static int
  257. ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize)
  258. {
  259. struct ogg *ogg = s->priv_data;
  260. int idx;
  261. struct ogg_stream *os;
  262. int complete = 0;
  263. int segp = 0, psize = 0;
  264. #if 0
  265. av_log (s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
  266. #endif
  267. do{
  268. idx = ogg->curidx;
  269. while (idx < 0){
  270. if (ogg_read_page (s, &idx) < 0)
  271. return -1;
  272. }
  273. os = ogg->streams + idx;
  274. #if 0
  275. av_log (s, AV_LOG_DEBUG,
  276. "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  277. idx, os->pstart, os->psize, os->segp, os->nsegs);
  278. #endif
  279. if (!os->codec){
  280. if (os->header < 0){
  281. os->codec = ogg_find_codec (os->buf, os->bufpos);
  282. if (!os->codec){
  283. os->header = 0;
  284. return 0;
  285. }
  286. }else{
  287. return 0;
  288. }
  289. }
  290. segp = os->segp;
  291. psize = os->psize;
  292. while (os->segp < os->nsegs){
  293. int ss = os->segments[os->segp++];
  294. os->psize += ss;
  295. if (ss < 255){
  296. complete = 1;
  297. break;
  298. }
  299. }
  300. if (!complete && os->segp == os->nsegs){
  301. ogg->curidx = -1;
  302. }
  303. }while (!complete);
  304. #if 0
  305. av_log (s, AV_LOG_DEBUG,
  306. "ogg_packet: idx %i, frame size %i, start %i\n",
  307. idx, os->psize, os->pstart);
  308. #endif
  309. ogg->curidx = idx;
  310. if (os->header < 0){
  311. int hdr = os->codec->header (s, idx);
  312. if (!hdr){
  313. os->header = os->seq;
  314. os->segp = segp;
  315. os->psize = psize;
  316. ogg->headers = 1;
  317. }else{
  318. os->pstart += os->psize;
  319. os->psize = 0;
  320. }
  321. }
  322. if (os->header > -1 && os->seq > os->header){
  323. os->pflags = 0;
  324. if (os->codec && os->codec->packet)
  325. os->codec->packet (s, idx);
  326. if (str)
  327. *str = idx;
  328. if (dstart)
  329. *dstart = os->pstart;
  330. if (dsize)
  331. *dsize = os->psize;
  332. os->pstart += os->psize;
  333. os->psize = 0;
  334. }
  335. os->seq++;
  336. if (os->segp == os->nsegs)
  337. ogg->curidx = -1;
  338. return 0;
  339. }
  340. static int
  341. ogg_get_headers (AVFormatContext * s)
  342. {
  343. struct ogg *ogg = s->priv_data;
  344. do{
  345. if (ogg_packet (s, NULL, NULL, NULL) < 0)
  346. return -1;
  347. }while (!ogg->headers);
  348. #if 0
  349. av_log (s, AV_LOG_DEBUG, "found headers\n");
  350. #endif
  351. return 0;
  352. }
  353. static uint64_t
  354. ogg_gptopts (AVFormatContext * s, int i, uint64_t gp)
  355. {
  356. struct ogg *ogg = s->priv_data;
  357. struct ogg_stream *os = ogg->streams + i;
  358. uint64_t pts = AV_NOPTS_VALUE;
  359. if(os->codec->gptopts){
  360. pts = os->codec->gptopts(s, i, gp);
  361. } else {
  362. pts = gp;
  363. }
  364. return pts;
  365. }
  366. static int
  367. ogg_get_length (AVFormatContext * s)
  368. {
  369. struct ogg *ogg = s->priv_data;
  370. int idx = -1, i;
  371. int64_t size, end;
  372. if(url_is_streamed(s->pb))
  373. return 0;
  374. // already set
  375. if (s->duration != AV_NOPTS_VALUE)
  376. return 0;
  377. size = url_fsize(s->pb);
  378. if(size < 0)
  379. return 0;
  380. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: size;
  381. ogg_save (s);
  382. url_fseek (s->pb, end, SEEK_SET);
  383. while (!ogg_read_page (s, &i)){
  384. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  385. ogg->streams[i].codec)
  386. idx = i;
  387. }
  388. if (idx != -1){
  389. s->streams[idx]->duration =
  390. ogg_gptopts (s, idx, ogg->streams[idx].granule);
  391. }
  392. ogg->size = size;
  393. ogg_restore (s, 0);
  394. return 0;
  395. }
  396. static int
  397. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  398. {
  399. struct ogg *ogg = s->priv_data;
  400. int i;
  401. ogg->curidx = -1;
  402. //linear headers seek from start
  403. if (ogg_get_headers (s) < 0){
  404. return -1;
  405. }
  406. for (i = 0; i < ogg->nstreams; i++)
  407. if (ogg->streams[i].header < 0)
  408. ogg->streams[i].codec = NULL;
  409. //linear granulepos seek from end
  410. ogg_get_length (s);
  411. //fill the extradata in the per codec callbacks
  412. return 0;
  413. }
  414. static int
  415. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  416. {
  417. struct ogg *ogg;
  418. struct ogg_stream *os;
  419. int idx = -1;
  420. int pstart, psize;
  421. //Get an ogg packet
  422. do{
  423. if (ogg_packet (s, &idx, &pstart, &psize) < 0)
  424. return AVERROR(EIO);
  425. }while (idx < 0 || !s->streams[idx]);
  426. ogg = s->priv_data;
  427. os = ogg->streams + idx;
  428. //Alloc a pkt
  429. if (av_new_packet (pkt, psize) < 0)
  430. return AVERROR(EIO);
  431. pkt->stream_index = idx;
  432. memcpy (pkt->data, os->buf + pstart, psize);
  433. if (os->lastgp != -1LL){
  434. pkt->pts = ogg_gptopts (s, idx, os->lastgp);
  435. os->lastgp = -1;
  436. }
  437. pkt->flags = os->pflags;
  438. return psize;
  439. }
  440. static int
  441. ogg_read_close (AVFormatContext * s)
  442. {
  443. struct ogg *ogg = s->priv_data;
  444. int i;
  445. for (i = 0; i < ogg->nstreams; i++){
  446. av_free (ogg->streams[i].buf);
  447. av_free (ogg->streams[i].private);
  448. }
  449. av_free (ogg->streams);
  450. return 0;
  451. }
  452. static int64_t
  453. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  454. int64_t pos_limit)
  455. {
  456. struct ogg *ogg = s->priv_data;
  457. ByteIOContext *bc = s->pb;
  458. int64_t pts = AV_NOPTS_VALUE;
  459. int i;
  460. url_fseek(bc, *pos_arg, SEEK_SET);
  461. while (url_ftell(bc) < pos_limit && !ogg_read_page (s, &i)) {
  462. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  463. ogg->streams[i].codec && i == stream_index) {
  464. pts = ogg_gptopts(s, i, ogg->streams[i].granule);
  465. // FIXME: this is the position of the packet after the one with above
  466. // pts.
  467. *pos_arg = url_ftell(bc);
  468. break;
  469. }
  470. }
  471. ogg_reset(ogg);
  472. return pts;
  473. }
  474. static int ogg_probe(AVProbeData *p)
  475. {
  476. if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
  477. p->buf[2] == 'g' && p->buf[3] == 'S' &&
  478. p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
  479. return AVPROBE_SCORE_MAX;
  480. else
  481. return 0;
  482. }
  483. AVInputFormat ogg_demuxer = {
  484. "ogg",
  485. NULL_IF_CONFIG_SMALL("Ogg"),
  486. sizeof (struct ogg),
  487. ogg_probe,
  488. ogg_read_header,
  489. ogg_read_packet,
  490. ogg_read_close,
  491. NULL,
  492. ogg_read_timestamp,
  493. .extensions = "ogg",
  494. };