/*
- * Copyright (C) 2004-2011 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>
*
* Licensed under the GPL v2. For licencing details see COPYING.
*/
-/** \file ogg_afh_common.c Functions common to ogg/vorbis and ogg/speex. */
+/** \file ogg_afh_common.c Functions common to all ogg/ codecs. */
#include <ogg/ogg.h>
#include <regex.h>
#include "error.h"
#include "string.h"
#include "ogg_afh_common.h"
-
+#include "fd.h"
/* Taken from decoder_example.c of libvorbis-1.2.3. */
static int process_packets_2_and_3(ogg_sync_state *oss,
* apparent at packetout.
*/
ogg_stream_pagein(stream, &page);
+ PARA_INFO_LOG("ogg page serial: %d\n",
+ ogg_page_serialno(&page));
while (i < 2) {
ret = ogg_stream_packetout(stream, &packet);
if (ret == 0)
break;
if (ret < 0)
return -E_STREAM_PACKETOUT;
- ret = ci->packet_callback(&packet, i + 1, afhi,
+ ret = ci->packet_callback(&packet, i + 1,
+ ogg_page_serialno(&page), afhi,
ci->private_data);
if (ret < 0)
return ret;
ret = -E_STREAM_PACKETOUT;
if (ogg_stream_packetout(&stream, &packet) != 1)
goto out;
- ret = ci->packet_callback(&packet, 0, afhi, ci->private_data);
+ ret = ci->packet_callback(&packet, 0, ogg_page_serialno(&page),
+ afhi, ci->private_data);
if (ret < 0)
goto out;
ret = process_packets_2_and_3(oss, &stream, afhi, ci);
if (ret < 0)
goto out;
- afhi->header_offset = 0;
- afhi->header_len = oss->returned;
ret = 1;
out:
ogg_stream_clear(&stream);
return ret;
}
-static void set_chunk_tv(int num_frames, int num_chunks, int frequency,
+static void set_chunk_tv(int frames_per_chunk, int frequency,
struct timeval *result)
{
- uint64_t x = (uint64_t)num_frames * 1000 * 1000
- / frequency / num_chunks;
+ uint64_t x = (uint64_t)frames_per_chunk * 1000 * 1000 / frequency;
result->tv_sec = x / 1000 / 1000;
result->tv_usec = x % (1000 * 1000);
- PARA_INFO_LOG("%d chunks, chunk time: %lums\n", num_chunks,
- tv2ms(result));
+ PARA_INFO_LOG("%d frames per chunk, chunk time: %lums\n",
+ frames_per_chunk, tv2ms(result));
}
/**
* given by \a map and \a numbytes and passes each packet to the callback
* defined by \a ci.
*
- * If the packet callback indicates success, the chunk table is built. Chunk
- * zero contains the first three ogg packets while all other chunks consist of
- * exactly one ogg page.
+ * If the packet callback indicates success and \a afhi is not \p NULL, the
+ * chunk table is built. Chunk zero contains the first three ogg packets while
+ * all other chunks consist of exactly one ogg page.
*
* \param map Audio file data.
* \param numbytes The length of \a map.
ret = process_ogg_packets(&oss, afhi, ci);
if (ret < 0)
goto out;
+ if (!afhi)
+ goto out;
+ afhi->header_len = oss.returned;
oss.returned = 0;
oss.fill = numbytes;
- /* count ogg packages and get duration of the file */
+ /* count ogg pages and get duration of the file */
for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
num_frames = ogg_page_granulepos(&op);
PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
afhi->seconds_total = num_frames / afhi->frequency;
/* use roughly one page per chunk */
frames_per_chunk = num_frames / i;
- PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
+ PARA_INFO_LOG("%" PRIu32 "seconds, %d frames/chunk\n",
afhi->seconds_total, frames_per_chunk);
ct_size = 250;
afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
int granule = ogg_page_granulepos(&op);
- while (granule > j * frames_per_chunk) {
+ while (granule >= (j + 1) * frames_per_chunk) {
j++;
if (j >= ct_size) {
ct_size *= 2;
}
}
afhi->chunks_total = j;
- set_chunk_tv(num_frames, j, afhi->frequency, &afhi->chunk_tv);
+ set_chunk_tv(frames_per_chunk, afhi->frequency, &afhi->chunk_tv);
ret = 0;
out:
ogg_sync_clear(&oss);
return ret;
}
+
+static int write_ogg_page(int fd, const ogg_page *op)
+{
+ int ret;
+
+ PARA_DEBUG_LOG("header/body: %li/%li\n", op->header_len, op->body_len);
+ ret = xwrite(fd, (const char *)op->header, op->header_len);
+ if (ret < 0)
+ return ret;
+ return xwrite(fd, (const char *)op->body, op->body_len);
+}
+
+/**
+ * Change meta tags of ogg files.
+ *
+ * \param map The (read-only) memory map of the input file.
+ * \param map_sz The size of the input file in bytes.
+ * \param fd The output file descriptor.
+ * \param meta_packet Codec-specific packet containing modified tags.
+ * \param meta_sz Size of the metadata packet.
+ *
+ * This function writes a new ogg file content using file descriptor \a fd,
+ * which must correspond to a file which has been opened for writing. The
+ * second packet is supposed to contain the metadata, and is replaced by \a
+ * meta_packet. This output file has to be closed by the caller.
+ *
+ * \return Standard.
+ */
+int ogg_rewrite_tags(const char *map, size_t map_sz, int fd,
+ char *meta_packet, size_t meta_sz)
+{
+ ogg_sync_state oss_in, oss_out;
+ ogg_stream_state stream_in, stream_out, *si = NULL, *so = NULL;
+ ogg_packet packet;
+ ogg_page op;
+ char *buf;
+ int serial, ret;
+ long len = map_sz;
+
+ ogg_sync_init(&oss_in);
+ ogg_sync_init(&oss_out);
+
+ ret = -E_OGG_SYNC;
+ buf = ogg_sync_buffer(&oss_in, len);
+ if (!buf)
+ goto out;
+ memcpy(buf, map, len);
+ ret = -E_OGG_SYNC;
+ if (ogg_sync_wrote(&oss_in, len) < 0)
+ goto out;
+ if (ogg_sync_pageout(&oss_in, &op) != 1)
+ goto out;
+ ret = ogg_page_serialno(&op);
+ serial = ret;
+
+ si = &stream_in;
+ ogg_stream_init(si, serial);
+ /* Packet #0 goes to an own page */
+ ret = -E_STREAM_PAGEIN;
+ if (ogg_stream_pagein(si, &op) < 0)
+ goto out;
+ ret = -E_STREAM_PACKETOUT;
+ if (ogg_stream_packetout(si, &packet) != 1)
+ goto out;
+ ret = -E_STREAM_PACKETIN;
+ so = &stream_out;
+ ogg_stream_init(so, serial);
+ if (ogg_stream_packetin(so, &packet) != 0)
+ goto out;
+ ret = ogg_stream_flush(so, &op);
+ assert(ret != 0);
+ /* packets have been flushed into the page. */
+ ret = write_ogg_page(fd, &op);
+ if (ret < 0)
+ goto out;
+ /*
+ * For all supported ogg/xxx audio formats the meta data packet is
+ * packet #1. Write out our modified version of this packet.
+ */
+ packet.packetno = 1;
+ packet.b_o_s = packet.e_o_s = 0;
+ packet.packet = (typeof(packet.packet))meta_packet;
+ packet.bytes = meta_sz;
+ ret = -E_STREAM_PACKETIN;
+ if (ogg_stream_packetin(so, &packet) != 0)
+ goto out;
+ /* Copy ogg packets, ignoring the meta data packet. */
+ for (;;) {
+ ret = ogg_stream_packetout(si, &packet);
+ if (ret == -1)
+ break;
+ if (ret != 1) {
+ ret = -E_STREAM_PAGEOUT;
+ if (ogg_sync_pageout(&oss_in, &op) < 0)
+ goto out;
+ ret = -E_STREAM_PAGEIN;
+ if (ogg_stream_pagein(si, &op))
+ goto out;
+ continue;
+ }
+ PARA_DEBUG_LOG("packet: bytes: %d, granule: %d, packetno: %d\n",
+ (int)packet.bytes, (int)packet.granulepos,
+ (int)packet.packetno);
+ /* ignore meta data packet which we replaced */
+ if (packet.packetno == 1)
+ continue;
+ ret = -E_STREAM_PACKETIN;
+ if (ogg_stream_packetin(so, &packet) != 0)
+ goto out;
+ /* only create a new ogg page if granulepos is valid */
+ if (packet.granulepos == -1)
+ continue;
+ /* force remaining packets into a page */
+ for (;;) {
+#ifdef HAVE_OGG_STREAM_FLUSH_FILL
+ ret = ogg_stream_flush_fill(so, &op, INT_MAX);
+#else
+ ret = ogg_stream_flush(so, &op);
+#endif
+ if (ret <= 0)
+ break;
+ PARA_DEBUG_LOG("writing page (%li bytes)\n",
+ op.header_len + op.body_len);
+ ret = write_ogg_page(fd, &op);
+ if (ret < 0)
+ goto out;
+ }
+ }
+ if (ogg_stream_flush(so, &op)) {
+ /* write remaining data */
+ ret = write_ogg_page(fd, &op);
+ if (ret < 0)
+ goto out;
+ }
+ ret = 1;
+out:
+ ogg_sync_clear(&oss_in);
+ ogg_sync_clear(&oss_out);
+ if (si)
+ ogg_stream_clear(si);
+ if (so)
+ ogg_stream_clear(so);
+ return ret;
+}