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.

647 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 "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. NULL
  37. };
  38. #if 0 // CONFIG_MUXERS
  39. static int
  40. ogg_write_header (AVFormatContext * avfcontext)
  41. {
  42. }
  43. static int
  44. ogg_write_packet (AVFormatContext * avfcontext, AVPacket * pkt)
  45. {
  46. }
  47. static int
  48. ogg_write_trailer (AVFormatContext * avfcontext)
  49. {
  50. }
  51. static AVOutputFormat ogg_oformat = {
  52. "ogg",
  53. "Ogg Vorbis",
  54. "audio/x-vorbis",
  55. "ogg",
  56. sizeof (OggContext),
  57. CODEC_ID_VORBIS,
  58. 0,
  59. ogg_write_header,
  60. ogg_write_packet,
  61. ogg_write_trailer,
  62. };
  63. #endif //CONFIG_MUXERS
  64. //FIXME We could avoid some structure duplication
  65. static int
  66. ogg_save (AVFormatContext * s)
  67. {
  68. ogg_t *ogg = s->priv_data;
  69. ogg_state_t *ost =
  70. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  71. int i;
  72. ost->pos = url_ftell (&s->pb);;
  73. ost->curidx = ogg->curidx;
  74. ost->next = ogg->state;
  75. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  76. for (i = 0; i < ogg->nstreams; i++){
  77. ogg_stream_t *os = ogg->streams + i;
  78. os->buf = av_malloc (os->bufsize);
  79. memset (os->buf, 0, os->bufsize);
  80. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  81. }
  82. ogg->state = ost;
  83. return 0;
  84. }
  85. static int
  86. ogg_restore (AVFormatContext * s, int discard)
  87. {
  88. ogg_t *ogg = s->priv_data;
  89. ByteIOContext *bc = &s->pb;
  90. ogg_state_t *ost = ogg->state;
  91. int i;
  92. if (!ost)
  93. return 0;
  94. ogg->state = ost->next;
  95. if (!discard){
  96. for (i = 0; i < ogg->nstreams; i++)
  97. av_free (ogg->streams[i].buf);
  98. url_fseek (bc, ost->pos, SEEK_SET);
  99. ogg->curidx = ost->curidx;
  100. memcpy (ogg->streams, ost->streams,
  101. ogg->nstreams * sizeof (*ogg->streams));
  102. }
  103. av_free (ost);
  104. return 0;
  105. }
  106. static int
  107. ogg_reset (ogg_t * ogg)
  108. {
  109. int i;
  110. for (i = 0; i < ogg->nstreams; i++){
  111. ogg_stream_t *os = ogg->streams + i;
  112. os->bufpos = 0;
  113. os->pstart = 0;
  114. os->psize = 0;
  115. os->granule = -1;
  116. os->lastgp = -1;
  117. os->nsegs = 0;
  118. os->segp = 0;
  119. }
  120. ogg->curidx = -1;
  121. return 0;
  122. }
  123. static ogg_codec_t *
  124. ogg_find_codec (uint8_t * buf, int size)
  125. {
  126. int i;
  127. for (i = 0; ogg_codecs[i]; i++)
  128. if (size >= ogg_codecs[i]->magicsize &&
  129. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  130. return ogg_codecs[i];
  131. return NULL;
  132. }
  133. static int
  134. ogg_find_stream (ogg_t * ogg, int serial)
  135. {
  136. int i;
  137. for (i = 0; i < ogg->nstreams; i++)
  138. if (ogg->streams[i].serial == serial)
  139. return i;
  140. return -1;
  141. }
  142. static int
  143. ogg_new_stream (AVFormatContext * s, uint32_t serial)
  144. {
  145. ogg_t *ogg = s->priv_data;
  146. int idx = ogg->nstreams++;
  147. AVStream *st;
  148. ogg_stream_t *os;
  149. ogg->streams = av_realloc (ogg->streams,
  150. ogg->nstreams * sizeof (*ogg->streams));
  151. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  152. os = ogg->streams + idx;
  153. os->serial = serial;
  154. os->bufsize = DECODER_BUFFER_SIZE;
  155. os->buf = av_malloc (os->bufsize);
  156. memset (os->buf, 0, os->bufsize);
  157. os->header = -1;
  158. st = av_new_stream (s, idx);
  159. if (!st)
  160. return AVERROR_NOMEM;
  161. av_set_pts_info(st, 64, 1, 1000000);
  162. st->start_time = 0;
  163. return idx;
  164. }
  165. static int
  166. ogg_read_page (AVFormatContext * s, int *str)
  167. {
  168. ByteIOContext *bc = &s->pb;
  169. ogg_t *ogg = s->priv_data;
  170. ogg_stream_t *os;
  171. int i = 0;
  172. int flags, nsegs;
  173. uint64_t gp;
  174. uint32_t serial;
  175. uint32_t seq;
  176. uint32_t crc;
  177. int size, idx;
  178. char sync[4];
  179. int sp = 0;
  180. if (get_buffer (bc, sync, 4) < 4)
  181. return -1;
  182. do{
  183. int c;
  184. if (sync[sp & 3] == 'O' &&
  185. sync[(sp + 1) & 3] == 'g' &&
  186. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  187. break;
  188. c = url_fgetc (bc);
  189. if (c < 0)
  190. return -1;
  191. sync[sp++ & 3] = c;
  192. }while (i++ < MAX_PAGE_SIZE);
  193. if (i >= MAX_PAGE_SIZE){
  194. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  195. return -1;
  196. }
  197. if (url_fgetc (bc) != 0) /* version */
  198. return -1;
  199. flags = url_fgetc (bc);
  200. gp = get_le64 (bc);
  201. serial = get_le32 (bc);
  202. seq = get_le32 (bc);
  203. crc = get_le32 (bc);
  204. nsegs = url_fgetc (bc);
  205. idx = ogg_find_stream (ogg, serial);
  206. if (idx < 0){
  207. idx = ogg_new_stream (s, serial);
  208. if (idx < 0)
  209. return -1;
  210. }
  211. os = ogg->streams + 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. memset (nb, 0, os->bufsize);
  234. memcpy (nb, os->buf, os->bufpos);
  235. av_free (os->buf);
  236. os->buf = nb;
  237. }
  238. if (get_buffer (bc, os->buf + os->bufpos, size) < size)
  239. return -1;
  240. os->lastgp = os->granule;
  241. os->bufpos += size;
  242. os->granule = gp;
  243. os->flags = flags;
  244. if (str)
  245. *str = idx;
  246. return 0;
  247. }
  248. static int
  249. ogg_packet (AVFormatContext * s, int *str)
  250. {
  251. ogg_t *ogg = s->priv_data;
  252. int idx;
  253. ogg_stream_t *os;
  254. int complete = 0;
  255. int segp = 0, psize = 0;
  256. #if 0
  257. av_log (s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
  258. #endif
  259. do{
  260. idx = ogg->curidx;
  261. while (idx < 0){
  262. if (ogg_read_page (s, &idx) < 0)
  263. return -1;
  264. }
  265. os = ogg->streams + idx;
  266. #if 0
  267. av_log (s, AV_LOG_DEBUG,
  268. "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  269. idx, os->pstart, os->psize, os->segp, os->nsegs);
  270. #endif
  271. if (!os->codec){
  272. if (os->header < 0){
  273. os->codec = ogg_find_codec (os->buf, os->bufpos);
  274. if (!os->codec){
  275. os->header = 0;
  276. return 0;
  277. }
  278. }else{
  279. return 0;
  280. }
  281. }
  282. segp = os->segp;
  283. psize = os->psize;
  284. while (os->segp < os->nsegs){
  285. int ss = os->segments[os->segp++];
  286. os->psize += ss;
  287. if (ss < 255){
  288. complete = 1;
  289. break;
  290. }
  291. }
  292. if (!complete && os->segp == os->nsegs){
  293. uint8_t *nb = av_malloc (os->bufsize);
  294. int size = os->bufpos - os->pstart;
  295. memset (nb, 0, os->bufsize);
  296. memcpy (nb, os->buf + os->pstart, size);
  297. av_free (os->buf);
  298. os->buf = nb;
  299. os->bufpos = size;
  300. os->pstart = 0;
  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. if (os->codec && os->codec->packet)
  324. os->codec->packet (s, idx);
  325. if (str)
  326. *str = idx;
  327. }
  328. os->seq++;
  329. if (os->segp == os->nsegs)
  330. ogg->curidx = -1;
  331. return 0;
  332. }
  333. static int
  334. ogg_get_headers (AVFormatContext * s)
  335. {
  336. ogg_t *ogg = s->priv_data;
  337. do{
  338. if (ogg_packet (s, NULL) < 0)
  339. return -1;
  340. }while (!ogg->headers);
  341. #if 0
  342. av_log (s, AV_LOG_DEBUG, "found headers\n");
  343. #endif
  344. return 0;
  345. }
  346. static uint64_t
  347. ogg_gptopts (AVFormatContext * s, int i, uint64_t gp)
  348. {
  349. ogg_t *ogg = s->priv_data;
  350. ogg_stream_t *os = ogg->streams + i;
  351. AVStream *st = s->streams[i];
  352. AVCodecContext *codec = st->codec;
  353. uint64_t pts = AV_NOPTS_VALUE;
  354. if(os->codec->gptopts){
  355. pts = os->codec->gptopts(s, i, gp);
  356. } else if (codec->codec_type == CODEC_TYPE_AUDIO){
  357. pts = gp * 1000000LL / codec->sample_rate;
  358. }else if (codec->codec_type == CODEC_TYPE_VIDEO){
  359. pts = gp;
  360. }
  361. return pts;
  362. }
  363. static int
  364. ogg_get_length (AVFormatContext * s)
  365. {
  366. ogg_t *ogg = s->priv_data;
  367. int idx = -1, i;
  368. if(s->pb.is_streamed)
  369. return 0;
  370. // already set
  371. if (s->duration != AV_NOPTS_VALUE)
  372. return 0;
  373. ogg_save (s);
  374. url_fseek (&s->pb, -MAX_PAGE_SIZE, SEEK_END);
  375. while (!ogg_read_page (s, &i)){
  376. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
  377. idx = i;
  378. }
  379. if (idx != -1){
  380. s->streams[idx]->duration =
  381. ogg_gptopts (s, idx, ogg->streams[idx].granule);
  382. }
  383. ogg->size = url_fsize(&s->pb);
  384. ogg_restore (s, 0);
  385. return 0;
  386. }
  387. static int
  388. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  389. {
  390. ogg_t *ogg = s->priv_data;
  391. ogg->curidx = -1;
  392. //linear headers seek from start
  393. if (ogg_get_headers (s) < 0){
  394. return -1;
  395. }
  396. //linear granulepos seek from end
  397. ogg_get_length (s);
  398. //fill the extradata in the per codec callbacks
  399. return 0;
  400. }
  401. static int
  402. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  403. {
  404. ogg_t *ogg;
  405. ogg_stream_t *os;
  406. int idx = -1;
  407. //Get an ogg packet
  408. do{
  409. if (ogg_packet (s, &idx) < 0)
  410. return AVERROR_IO;
  411. }while (idx < 0 || !s->streams[idx]);
  412. ogg = s->priv_data;
  413. os = ogg->streams + idx;
  414. //Alloc a pkt
  415. if (av_new_packet (pkt, os->psize) < 0)
  416. return AVERROR_IO;
  417. pkt->stream_index = idx;
  418. memcpy (pkt->data, os->buf + os->pstart, os->psize);
  419. if (os->lastgp != -1LL){
  420. pkt->pts = ogg_gptopts (s, idx, os->lastgp);
  421. os->lastgp = -1;
  422. }
  423. //next
  424. os->pstart += os->psize;
  425. os->psize = 0;
  426. return os->psize;
  427. }
  428. static int
  429. ogg_read_close (AVFormatContext * s)
  430. {
  431. ogg_t *ogg = s->priv_data;
  432. int i;
  433. for (i = 0; i < ogg->nstreams; i++){
  434. av_free (ogg->streams[i].buf);
  435. av_free (ogg->streams[i].private);
  436. av_freep (&s->streams[i]->codec->extradata);
  437. }
  438. av_free (ogg->streams);
  439. return 0;
  440. }
  441. static int
  442. ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
  443. int flags)
  444. {
  445. ogg_t *ogg = s->priv_data;
  446. ByteIOContext *bc = &s->pb;
  447. uint64_t min = 0, max = ogg->size;
  448. uint64_t tmin = 0, tmax = s->duration;
  449. int64_t pts = AV_NOPTS_VALUE;
  450. ogg_save (s);
  451. while (min <= max){
  452. uint64_t p = min + (max - min) * (target_ts - tmin) / (tmax - tmin);
  453. int i = -1;
  454. url_fseek (bc, p, SEEK_SET);
  455. while (!ogg_read_page (s, &i)){
  456. if (ogg->streams[i].granule != 0 && ogg->streams[i].granule != -1)
  457. break;
  458. }
  459. if (i == -1)
  460. break;
  461. pts = ogg_gptopts (s, i, ogg->streams[i].granule);
  462. p = url_ftell (bc);
  463. if (ABS (pts - target_ts) < 1000000LL)
  464. break;
  465. if (pts > target_ts){
  466. max = p;
  467. tmax = pts;
  468. }else{
  469. min = p;
  470. tmin = pts;
  471. }
  472. }
  473. if (ABS (pts - target_ts) < 1000000LL){
  474. ogg_restore (s, 1);
  475. ogg_reset (ogg);
  476. }else{
  477. ogg_restore (s, 0);
  478. pts = AV_NOPTS_VALUE;
  479. }
  480. return pts;
  481. #if 0
  482. //later...
  483. int64_t pos;
  484. if (av_seek_frame_binary (s, stream_index, target_ts, flags) < 0)
  485. return -1;
  486. pos = url_ftell (&s->pb);
  487. ogg_read_timestamp (s, stream_index, &pos, pos - 1);
  488. #endif
  489. }
  490. #if 0
  491. static int64_t
  492. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  493. int64_t pos_limit)
  494. {
  495. ogg_t *ogg = s->priv_data;
  496. ByteIOContext *bc = &s->pb;
  497. int64_t pos, pts;
  498. if (*pos_arg < 0)
  499. return AV_NOPTS_VALUE;
  500. pos = *pos_arg;
  501. }
  502. #endif
  503. static AVInputFormat ogg_iformat = {
  504. "ogg",
  505. "Ogg",
  506. sizeof (ogg_t),
  507. NULL,
  508. ogg_read_header,
  509. ogg_read_packet,
  510. ogg_read_close,
  511. ogg_read_seek,
  512. // ogg_read_timestamp,
  513. .extensions = "ogg",
  514. };
  515. int
  516. ogg_init (void)
  517. {
  518. #if 0 // CONFIG_MUXERS
  519. av_register_output_format (&ogg_oformat);
  520. #endif
  521. av_register_input_format (&ogg_iformat);
  522. return 0;
  523. }