mkvtoolnix/queue.cpp

133 lines
2.6 KiB
C++
Raw Normal View History

2003-02-16 11:44:19 +00:00
/*
mkvmerge -- utility for splicing together matroska files
from component media subtypes
queue.cpp
Written by Moritz Bunkus <moritz@bunkus.org>
Distributed under the GPL
see the file COPYING for details
or visit http://www.gnu.org/copyleft/gpl.html
*/
2003-02-16 12:17:11 +00:00
/*!
\file
\version \$Id: queue.cpp,v 1.16 2003/04/20 21:18:51 mosu Exp $
2003-02-16 12:17:11 +00:00
\brief packet queueing class used by every packetizer
\author Moritz Bunkus <moritz @ bunkus.org>
*/
2003-02-16 11:44:19 +00:00
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <typeinfo>
#include "common.h"
#include "queue.h"
#ifdef DMALLOC
#include <dmalloc.h>
#endif
q_c::q_c() {
2003-02-16 11:44:19 +00:00
first = NULL;
current = NULL;
}
q_c::~q_c() {
q_page_t *qpage, *tmppage;
qpage = first;
while (qpage) {
if (qpage->pack != NULL) {
if (qpage->pack->data != NULL)
free(qpage->pack->data);
free(qpage->pack);
}
tmppage = qpage->next;
free(qpage);
qpage = tmppage;
}
}
void q_c::add_packet(unsigned char *data, int length, int64_t timecode,
int64_t bref, int64_t fref, int64_t duration) {
2003-02-16 11:44:19 +00:00
q_page_t *qpage;
if (data == NULL)
return;
2003-04-18 08:42:20 +00:00
if (timecode < 0)
2003-04-17 12:29:08 +00:00
die("timecode < 0");
2003-02-16 11:44:19 +00:00
qpage = (q_page_t *)malloc(sizeof(q_page_t));
if (qpage == NULL)
die("malloc");
qpage->pack = (packet_t *)malloc(sizeof(packet_t));
if (qpage->pack == NULL)
die("malloc");
memset(qpage->pack, 0, sizeof(packet_t));
qpage->pack->data = (unsigned char *)malloc(length);
2003-02-16 11:44:19 +00:00
if (qpage->pack->data == NULL)
die("malloc");
memcpy(qpage->pack->data, data, length);
qpage->pack->length = length;
2003-04-18 08:42:20 +00:00
qpage->pack->timecode = timecode;
2003-02-27 19:51:53 +00:00
qpage->pack->bref = bref;
qpage->pack->fref = fref;
qpage->pack->duration = duration;
2003-02-16 17:04:39 +00:00
qpage->pack->source = this;
2003-02-16 11:44:19 +00:00
qpage->next = NULL;
if (current != NULL)
current->next = qpage;
if (first == NULL)
first = qpage;
current = qpage;
}
packet_t *q_c::get_packet() {
packet_t *pack;
q_page_t *qpage;
if (first && first->pack) {
pack = first->pack;
qpage = first->next;
if (qpage == NULL)
current = NULL;
free(first);
first = qpage;
return pack;
}
return NULL;
}
int q_c::packet_available() {
if ((first == NULL) || (first->pack == NULL))
return 0;
else
return 1;
}
int64_t q_c::get_smallest_timecode() {
2003-02-16 11:44:19 +00:00
if (first != NULL)
2003-04-18 08:42:20 +00:00
return first->pack->timecode;
2003-02-16 11:44:19 +00:00
else
return 0x0FFFFFFFLL;
2003-02-16 11:44:19 +00:00
}
long q_c::get_queued_bytes() {
long bytes;
q_page_t *cur;
bytes = 0;
cur = first;
while (cur != NULL) {
if (cur->pack != NULL)
bytes += cur->pack->length;
cur = cur->next;
}
return bytes;
}