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.

700 lines
15KB

  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 "ogg2.h"
  29. #include "avformat.h"
  30. #define MAX_PAGE_SIZE 65307
  31. #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
  32. static ogg_codec_t *ogg_codecs[] = {
  33. &vorbis_codec,
  34. &theora_codec,
  35. &flac_codec,
  36. &old_flac_codec,
  37. &ogm_video_codec,
  38. &ogm_audio_codec,
  39. &ogm_old_codec,
  40. NULL
  41. };
  42. #if 0 // CONFIG_MUXERS
  43. static int
  44. ogg_write_header (AVFormatContext * avfcontext)
  45. {
  46. }
  47. static int
  48. ogg_write_packet (AVFormatContext * avfcontext, AVPacket * pkt)
  49. {
  50. }
  51. static int
  52. ogg_write_trailer (AVFormatContext * avfcontext)
  53. {
  54. }
  55. AVOutputFormat ogg_muxer = {
  56. "ogg",
  57. "Ogg format",
  58. "application/ogg",
  59. "ogg",
  60. sizeof (OggContext),
  61. CODEC_ID_VORBIS,
  62. 0,
  63. ogg_write_header,
  64. ogg_write_packet,
  65. ogg_write_trailer,
  66. };
  67. #endif //CONFIG_MUXERS
  68. //FIXME We could avoid some structure duplication
  69. static int
  70. ogg_save (AVFormatContext * s)
  71. {
  72. ogg_t *ogg = s->priv_data;
  73. ogg_state_t *ost =
  74. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  75. int i;
  76. ost->pos = url_ftell (&s->pb);;
  77. ost->curidx = ogg->curidx;
  78. ost->next = ogg->state;
  79. ost->nstreams = ogg->nstreams;
  80. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  81. for (i = 0; i < ogg->nstreams; i++){
  82. ogg_stream_t *os = ogg->streams + i;
  83. os->buf = av_malloc (os->bufsize);
  84. memset (os->buf, 0, os->bufsize);
  85. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  86. }
  87. ogg->state = ost;
  88. return 0;
  89. }
  90. static int
  91. ogg_restore (AVFormatContext * s, int discard)
  92. {
  93. ogg_t *ogg = s->priv_data;
  94. ByteIOContext *bc = &s->pb;
  95. ogg_state_t *ost = ogg->state;
  96. int i;
  97. if (!ost)
  98. return 0;
  99. ogg->state = ost->next;
  100. if (!discard){
  101. for (i = 0; i < ogg->nstreams; i++)
  102. av_free (ogg->streams[i].buf);
  103. url_fseek (bc, ost->pos, SEEK_SET);
  104. ogg->curidx = ost->curidx;
  105. ogg->nstreams = ost->nstreams;
  106. memcpy(ogg->streams, ost->streams,
  107. ost->nstreams * sizeof(*ogg->streams));
  108. }
  109. av_free (ost);
  110. return 0;
  111. }
  112. static int
  113. ogg_reset (ogg_t * ogg)
  114. {
  115. int i;
  116. for (i = 0; i < ogg->nstreams; i++){
  117. ogg_stream_t *os = ogg->streams + i;
  118. os->bufpos = 0;
  119. os->pstart = 0;
  120. os->psize = 0;
  121. os->granule = -1;
  122. os->lastgp = -1;
  123. os->nsegs = 0;
  124. os->segp = 0;
  125. }
  126. ogg->curidx = -1;
  127. return 0;
  128. }
  129. static ogg_codec_t *
  130. ogg_find_codec (uint8_t * buf, int size)
  131. {
  132. int i;
  133. for (i = 0; ogg_codecs[i]; i++)
  134. if (size >= ogg_codecs[i]->magicsize &&
  135. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  136. return ogg_codecs[i];
  137. return NULL;
  138. }
  139. static int
  140. ogg_find_stream (ogg_t * ogg, int serial)
  141. {
  142. int i;
  143. for (i = 0; i < ogg->nstreams; i++)
  144. if (ogg->streams[i].serial == serial)
  145. return i;
  146. return -1;
  147. }
  148. static int
  149. ogg_new_stream (AVFormatContext * s, uint32_t serial)
  150. {
  151. ogg_t *ogg = s->priv_data;
  152. int idx = ogg->nstreams++;
  153. AVStream *st;
  154. ogg_stream_t *os;
  155. ogg->streams = av_realloc (ogg->streams,
  156. ogg->nstreams * sizeof (*ogg->streams));
  157. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  158. os = ogg->streams + idx;
  159. os->serial = serial;
  160. os->bufsize = DECODER_BUFFER_SIZE;
  161. os->buf = av_malloc(os->bufsize);
  162. os->header = -1;
  163. st = av_new_stream (s, idx);
  164. if (!st)
  165. return AVERROR(ENOMEM);
  166. av_set_pts_info(st, 64, 1, 1000000);
  167. return idx;
  168. }
  169. static int
  170. ogg_new_buf(ogg_t *ogg, int idx)
  171. {
  172. ogg_stream_t *os = ogg->streams + idx;
  173. uint8_t *nb = av_malloc(os->bufsize);
  174. int size = os->bufpos - os->pstart;
  175. if(os->buf){
  176. memcpy(nb, os->buf + os->pstart, size);
  177. av_free(os->buf);
  178. }
  179. os->buf = nb;
  180. os->bufpos = size;
  181. os->pstart = 0;
  182. return 0;
  183. }
  184. static int
  185. ogg_read_page (AVFormatContext * s, int *str)
  186. {
  187. ByteIOContext *bc = &s->pb;
  188. ogg_t *ogg = s->priv_data;
  189. ogg_stream_t *os;
  190. int i = 0;
  191. int flags, nsegs;
  192. uint64_t gp;
  193. uint32_t serial;
  194. uint32_t seq;
  195. uint32_t crc;
  196. int size, idx;
  197. uint8_t sync[4];
  198. int sp = 0;
  199. if (get_buffer (bc, sync, 4) < 4)
  200. return -1;
  201. do{
  202. int c;
  203. if (sync[sp & 3] == 'O' &&
  204. sync[(sp + 1) & 3] == 'g' &&
  205. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  206. break;
  207. c = url_fgetc (bc);
  208. if (c < 0)
  209. return -1;
  210. sync[sp++ & 3] = c;
  211. }while (i++ < MAX_PAGE_SIZE);
  212. if (i >= MAX_PAGE_SIZE){
  213. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  214. return -1;
  215. }
  216. if (url_fgetc (bc) != 0) /* version */
  217. return -1;
  218. flags = url_fgetc (bc);
  219. gp = get_le64 (bc);
  220. serial = get_le32 (bc);
  221. seq = get_le32 (bc);
  222. crc = get_le32 (bc);
  223. nsegs = url_fgetc (bc);
  224. idx = ogg_find_stream (ogg, serial);
  225. if (idx < 0){
  226. idx = ogg_new_stream (s, serial);
  227. if (idx < 0)
  228. return -1;
  229. }
  230. os = ogg->streams + idx;
  231. if(os->psize > 0)
  232. ogg_new_buf(ogg, idx);
  233. if (get_buffer (bc, os->segments, nsegs) < nsegs)
  234. return -1;
  235. os->nsegs = nsegs;
  236. os->segp = 0;
  237. size = 0;
  238. for (i = 0; i < nsegs; i++)
  239. size += os->segments[i];
  240. if (flags & OGG_FLAG_CONT){
  241. if (!os->psize){
  242. while (os->segp < os->nsegs){
  243. int seg = os->segments[os->segp++];
  244. os->pstart += seg;
  245. if (seg < 255)
  246. break;
  247. }
  248. }
  249. }else{
  250. os->psize = 0;
  251. }
  252. if (os->bufsize - os->bufpos < size){
  253. uint8_t *nb = av_malloc (os->bufsize *= 2);
  254. memcpy (nb, os->buf, os->bufpos);
  255. av_free (os->buf);
  256. os->buf = nb;
  257. }
  258. if (get_buffer (bc, os->buf + os->bufpos, size) < size)
  259. return -1;
  260. os->lastgp = os->granule;
  261. os->bufpos += size;
  262. os->granule = gp;
  263. os->flags = flags;
  264. if (str)
  265. *str = idx;
  266. return 0;
  267. }
  268. static int
  269. ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize)
  270. {
  271. ogg_t *ogg = s->priv_data;
  272. int idx;
  273. ogg_stream_t *os;
  274. int complete = 0;
  275. int segp = 0, psize = 0;
  276. #if 0
  277. av_log (s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
  278. #endif
  279. do{
  280. idx = ogg->curidx;
  281. while (idx < 0){
  282. if (ogg_read_page (s, &idx) < 0)
  283. return -1;
  284. }
  285. os = ogg->streams + idx;
  286. #if 0
  287. av_log (s, AV_LOG_DEBUG,
  288. "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  289. idx, os->pstart, os->psize, os->segp, os->nsegs);
  290. #endif
  291. if (!os->codec){
  292. if (os->header < 0){
  293. os->codec = ogg_find_codec (os->buf, os->bufpos);
  294. if (!os->codec){
  295. os->header = 0;
  296. return 0;
  297. }
  298. }else{
  299. return 0;
  300. }
  301. }
  302. segp = os->segp;
  303. psize = os->psize;
  304. while (os->segp < os->nsegs){
  305. int ss = os->segments[os->segp++];
  306. os->psize += ss;
  307. if (ss < 255){
  308. complete = 1;
  309. break;
  310. }
  311. }
  312. if (!complete && os->segp == os->nsegs){
  313. ogg->curidx = -1;
  314. }
  315. }while (!complete);
  316. #if 0
  317. av_log (s, AV_LOG_DEBUG,
  318. "ogg_packet: idx %i, frame size %i, start %i\n",
  319. idx, os->psize, os->pstart);
  320. #endif
  321. ogg->curidx = idx;
  322. if (os->header < 0){
  323. int hdr = os->codec->header (s, idx);
  324. if (!hdr){
  325. os->header = os->seq;
  326. os->segp = segp;
  327. os->psize = psize;
  328. ogg->headers = 1;
  329. }else{
  330. os->pstart += os->psize;
  331. os->psize = 0;
  332. }
  333. }
  334. if (os->header > -1 && os->seq > os->header){
  335. if (os->codec && os->codec->packet)
  336. os->codec->packet (s, idx);
  337. if (str)
  338. *str = idx;
  339. if (dstart)
  340. *dstart = os->pstart;
  341. if (dsize)
  342. *dsize = os->psize;
  343. os->pstart += os->psize;
  344. os->psize = 0;
  345. }
  346. os->seq++;
  347. if (os->segp == os->nsegs)
  348. ogg->curidx = -1;
  349. return 0;
  350. }
  351. static int
  352. ogg_get_headers (AVFormatContext * s)
  353. {
  354. ogg_t *ogg = s->priv_data;
  355. do{
  356. if (ogg_packet (s, NULL, NULL, NULL) < 0)
  357. return -1;
  358. }while (!ogg->headers);
  359. #if 0
  360. av_log (s, AV_LOG_DEBUG, "found headers\n");
  361. #endif
  362. return 0;
  363. }
  364. static uint64_t
  365. ogg_gptopts (AVFormatContext * s, int i, uint64_t gp)
  366. {
  367. ogg_t *ogg = s->priv_data;
  368. ogg_stream_t *os = ogg->streams + i;
  369. uint64_t pts = AV_NOPTS_VALUE;
  370. if(os->codec->gptopts){
  371. pts = os->codec->gptopts(s, i, gp);
  372. } else {
  373. pts = gp;
  374. }
  375. return pts;
  376. }
  377. static int
  378. ogg_get_length (AVFormatContext * s)
  379. {
  380. ogg_t *ogg = s->priv_data;
  381. int idx = -1, i;
  382. offset_t size, end;
  383. if(s->pb.is_streamed)
  384. return 0;
  385. // already set
  386. if (s->duration != AV_NOPTS_VALUE)
  387. return 0;
  388. size = url_fsize(&s->pb);
  389. if(size < 0)
  390. return 0;
  391. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: size;
  392. ogg_save (s);
  393. url_fseek (&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. idx = i;
  398. }
  399. if (idx != -1){
  400. s->streams[idx]->duration =
  401. ogg_gptopts (s, idx, ogg->streams[idx].granule);
  402. }
  403. ogg->size = size;
  404. ogg_restore (s, 0);
  405. ogg_save (s);
  406. while (!ogg_read_page (s, &i)) {
  407. if (i == idx && ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
  408. break;
  409. }
  410. if (i == idx) {
  411. s->streams[idx]->start_time = ogg_gptopts (s, idx, ogg->streams[idx].granule);
  412. s->streams[idx]->duration -= s->streams[idx]->start_time;
  413. }
  414. ogg_restore (s, 0);
  415. return 0;
  416. }
  417. static int
  418. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  419. {
  420. ogg_t *ogg = s->priv_data;
  421. ogg->curidx = -1;
  422. //linear headers seek from start
  423. if (ogg_get_headers (s) < 0){
  424. return -1;
  425. }
  426. //linear granulepos seek from end
  427. ogg_get_length (s);
  428. //fill the extradata in the per codec callbacks
  429. return 0;
  430. }
  431. static int
  432. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  433. {
  434. ogg_t *ogg;
  435. ogg_stream_t *os;
  436. int idx = -1;
  437. int pstart, psize;
  438. //Get an ogg packet
  439. do{
  440. if (ogg_packet (s, &idx, &pstart, &psize) < 0)
  441. return AVERROR(EIO);
  442. }while (idx < 0 || !s->streams[idx]);
  443. ogg = s->priv_data;
  444. os = ogg->streams + idx;
  445. //Alloc a pkt
  446. if (av_new_packet (pkt, psize) < 0)
  447. return AVERROR(EIO);
  448. pkt->stream_index = idx;
  449. memcpy (pkt->data, os->buf + pstart, psize);
  450. if (os->lastgp != -1LL){
  451. pkt->pts = ogg_gptopts (s, idx, os->lastgp);
  452. os->lastgp = -1;
  453. }
  454. return psize;
  455. }
  456. static int
  457. ogg_read_close (AVFormatContext * s)
  458. {
  459. ogg_t *ogg = s->priv_data;
  460. int i;
  461. for (i = 0; i < ogg->nstreams; i++){
  462. av_free (ogg->streams[i].buf);
  463. av_free (ogg->streams[i].private);
  464. }
  465. av_free (ogg->streams);
  466. return 0;
  467. }
  468. static int
  469. ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
  470. int flags)
  471. {
  472. AVStream *st = s->streams[stream_index];
  473. ogg_t *ogg = s->priv_data;
  474. ByteIOContext *bc = &s->pb;
  475. uint64_t min = 0, max = ogg->size;
  476. uint64_t tmin = st->start_time, tmax = st->start_time + st->duration;
  477. int64_t pts = AV_NOPTS_VALUE;
  478. ogg_save (s);
  479. if ((uint64_t)target_ts < tmin || target_ts < 0)
  480. target_ts = tmin;
  481. while (min <= max && tmin < tmax){
  482. uint64_t p = min + (max - min) * (target_ts - tmin) / (tmax - tmin);
  483. int i = -1;
  484. url_fseek (bc, p, SEEK_SET);
  485. while (!ogg_read_page (s, &i)){
  486. if (i == stream_index && ogg->streams[i].granule != 0 &&
  487. ogg->streams[i].granule != -1)
  488. break;
  489. }
  490. if (i == -1)
  491. break;
  492. pts = ogg_gptopts (s, i, ogg->streams[i].granule);
  493. p = url_ftell (bc);
  494. if (FFABS (pts - target_ts) * st->time_base.num < st->time_base.den)
  495. break;
  496. if (pts > target_ts){
  497. if (max == p && tmax == pts) {
  498. // probably our tmin is wrong, causing us to always end up too late in the file
  499. tmin = (target_ts + tmin + 1) / 2;
  500. if (tmin == target_ts) {
  501. url_fseek(bc, min, SEEK_SET);
  502. break;
  503. }
  504. }
  505. max = p;
  506. tmax = pts;
  507. }else{
  508. if (min == p && tmin == pts) {
  509. // probably our tmax is wrong, causing us to always end up too early in the file
  510. tmax = (target_ts + tmax) / 2;
  511. if (tmax == target_ts) {
  512. url_fseek(bc, max, SEEK_SET);
  513. break;
  514. }
  515. }
  516. min = p;
  517. tmin = pts;
  518. }
  519. }
  520. if (FFABS (pts - target_ts) * st->time_base.num < st->time_base.den){
  521. ogg_restore (s, 1);
  522. ogg_reset (ogg);
  523. }else{
  524. ogg_restore (s, 0);
  525. pts = AV_NOPTS_VALUE;
  526. }
  527. av_update_cur_dts(s, st, pts);
  528. return 0;
  529. #if 0
  530. //later...
  531. int64_t pos;
  532. if (av_seek_frame_binary (s, stream_index, target_ts, flags) < 0)
  533. return -1;
  534. pos = url_ftell (&s->pb);
  535. ogg_read_timestamp (s, stream_index, &pos, pos - 1);
  536. #endif
  537. }
  538. #if 0
  539. static int64_t
  540. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  541. int64_t pos_limit)
  542. {
  543. ogg_t *ogg = s->priv_data;
  544. ByteIOContext *bc = &s->pb;
  545. int64_t pos, pts;
  546. if (*pos_arg < 0)
  547. return AV_NOPTS_VALUE;
  548. pos = *pos_arg;
  549. }
  550. #endif
  551. static int ogg_probe(AVProbeData *p)
  552. {
  553. if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
  554. p->buf[2] == 'g' && p->buf[3] == 'S' &&
  555. p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
  556. return AVPROBE_SCORE_MAX;
  557. else
  558. return 0;
  559. }
  560. AVInputFormat ogg_demuxer = {
  561. "ogg",
  562. "Ogg",
  563. sizeof (ogg_t),
  564. ogg_probe,
  565. ogg_read_header,
  566. ogg_read_packet,
  567. ogg_read_close,
  568. ogg_read_seek,
  569. // ogg_read_timestamp,
  570. .extensions = "ogg",
  571. };