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.

623 lines
14KB

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