summaryrefslogtreecommitdiff
path: root/src/mov_encoder_writer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/mov_encoder_writer.cc')
-rw-r--r--src/mov_encoder_writer.cc195
1 files changed, 140 insertions, 55 deletions
diff --git a/src/mov_encoder_writer.cc b/src/mov_encoder_writer.cc
index f408417..bc9302a 100644
--- a/src/mov_encoder_writer.cc
+++ b/src/mov_encoder_writer.cc
@@ -44,10 +44,11 @@ using namespace std;
#include <time.h>
+#include "multiplexer.h"
+
MovEncoderWriter::MovEncoderWriter(const char* cpr,
- FramePriorityQueue *q,
- sem_t *s,
- pthread_mutex_t *m,
+ FramePriorityQueue *v_q, pthread_mutex_t *v_m, sem_t *v_s,
+ FramePriorityQueue *a_q, pthread_mutex_t *a_m, sem_t *a_s,
Info *i)
{
info = i;
@@ -80,12 +81,21 @@ MovEncoderWriter::MovEncoderWriter(const char* cpr,
file = new File(fname, "mpg", info);
- sem = s;
- queue = q;
- frame_number = 0;
- mutex = m;
+ video_queue = v_q;
+ video_sem = v_s;
+ video_mutex = v_m;
+
+ audio_queue = a_q;
+ audio_sem = a_s;
+ audio_mutex = a_m;
+
+ video_frame_number = 0;
+ audio_frame_number = 0;
running = true;
+
+ // empty_timecode_struc(&SCR);
+ // timestamp = 0.0;
}
MovEncoderWriter::~MovEncoderWriter()
@@ -94,74 +104,128 @@ MovEncoderWriter::~MovEncoderWriter()
delete file;
}
-//#define WRITE_DV 1
void MovEncoderWriter::thread_main()
{
info->info("MovEncoderWriter::run");
- Frame *frame;
+ Multiplexer multiplexer(file, info, &running,
+ video_queue, video_mutex, video_sem,
+ audio_queue, audio_mutex, audio_sem);
+ multiplexer.multiplex();
-#ifndef WRITE_DV
-
- write_header();
-
-#endif/*WRITE_DV*/
-
- while(running) {
- sem_wait(sem);
+#if 0
+ int wrote = 0;
+ while(running ) {
+ sem_wait(audio_sem);
// Lock output mutex
- pthread_mutex_lock(mutex);
- frame = queue->top();
- if(frame && frame->number == frame_number) queue->pop();
- // poolsize = queue->size();
- pthread_mutex_unlock(mutex);
+ pthread_mutex_lock(audio_mutex);
+ audio_frame = audio_queue->top();
+ if(audio_frame && audio_frame->number == audio_frame_number) audio_queue->pop();
+ pthread_mutex_unlock(audio_mutex);
// Unlock output mutex
- int wrote = 0;
- while(frame && (frame->number == frame_number)) {
+ while(audio_frame && (audio_frame->number == audio_frame_number)) {
+ if(file->Write(audio_frame->data, audio_frame->size) == -1) {
+ info->error("File write returned -1.");
+ return;
+ }
- int ret = 0;
+ delete audio_frame;
+ audio_frame = NULL;
+
+ wrote ++;
+ audio_frame_number++;
+
+ // Lock output mutex
+ pthread_mutex_lock(audio_mutex);
+ audio_frame = audio_queue->top();
+ if(audio_frame && audio_frame->number == audio_frame_number) audio_queue->pop();
+ pthread_mutex_unlock(audio_mutex);
+ // Unlock output mutex
+ }
+ }
+#endif/*0*/
+
+ // info->info("Wrote %d mpeg packets.", wrote);
+ info->info("MovEncoderWriter::stop");
+}
+
-#ifndef WRITE_DV
- if(frame->number%2 == 1) write_audio_header((unsigned short int)frame->size);
- else write_video_header((unsigned short int)frame->size);
- ret = file->Write(frame->data, frame->size);
-#else/*WRITE_DV*/
- if(frame->number%2 == 0) ret = file->Write(frame->data, frame->size);
-#endif/*WRITE_DV*/
- frame_number++;
- wrote ++;
- delete frame;
- if(ret == -1) {
- info->error("File write returned -1.");
- return;
- }
- // Lock output mutex
- pthread_mutex_lock(mutex);
- frame = queue->top();
- if(frame && frame->number == frame_number) queue->pop();
- // poolsize = queue->size();
- pthread_mutex_unlock(mutex);
- // Unlock output mutex
- }
- }
- info->info("MovEncoderWriter::stop");
-}
-void MovEncoderWriter::write_header()
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+#if 0
+void MovEncoderWriter::write_system_header(unsigned int audio_size, unsigned int video_size)
{
+ /*
+ Sys_header_struc sys_header;
+
+ unsigned int mux_rate = (audio_size + video_size) * 25;
+
+ create_sys_header(&sys_header,
+ mux_rate, //unsigned int rate_bound,
+ 1, //unsigned char audio_bound,
+ 1, //unsigned char fixed,
+ 1, //unsigned char CSPS,
+ 1, //unsigned char audio_lock,
+ 1, //unsigned char video_lock,
+ 1, //unsigned char video_bound,
+
+ AUDIO_STR_0, //unsigned char stream1,
+ 0, //unsigned char buffer1_scale,
+ audio_size, //unsigned int buffer1_size,
+ // audio_size/128, //unsigned int buffer1_size,
+
+ VIDEO_STR_0, //unsigned char stream2,
+ 1, //unsigned char buffer2_scale,
+ // video_size/1024, //unsigned int buffer2_size,
+ video_size, //unsigned int buffer2_size,
+
+ // We both have audio *and* video
+ STREAMS_BOTH);//unsigned int which_streams
+
+// create_sys_header (sys_header, mux_rate, 1, 1, 1, 1, 1, 1,
+// AUDIO_STR_0, 0, audio_size/128,
+// VIDEO_STR_0, 1, video_size/1024, STREAMS_BOTH );
+
+ file->Write(sys_header.buf, sizeof(sys_header.buf));
+*/
+ /**
+ * My shot at a valid system header
+ */
+ /*
// PACK
char pack_start_code[] = {
0x00, 0x00, 0x01, 0xBA,
@@ -177,6 +241,7 @@ void MovEncoderWriter::write_header()
};
file->Write(pack_data, sizeof(pack_data));
+ */
/*
// SYSTEM
char system_header_start_code[] = {
@@ -216,6 +281,28 @@ void MovEncoderWriter::write_header()
*/
}
+void MovEncoderWriter::write_packet_header(unsigned int audio_size, unsigned int video_size)
+{
+ /*
+ Pack_struc pack;
+ timestamp += 1.0;
+ make_timecode(timestamp, &SCR);
+ // Timecode_struc SCR;
+ unsigned int mux_rate = (audio_size + video_size) * 25;
+ // SCR.
+
+ create_pack(&pack, mux_rate, &SCR);
+
+ file->Write(pack.buf, sizeof(pack.buf));
+
+ unsigned char timestampbuf[32];
+ unsigned char *i = timestampbuf;
+ buffer_timecode (&SCR, MARKER_JUST_PTS, &i);
+
+ file->Write(timestampbuf, (int)i - (int)timestampbuf);
+ */
+}
+
void MovEncoderWriter::write_video_header(unsigned short int psize)
{
// PES Header startcode
@@ -283,10 +370,7 @@ void MovEncoderWriter::write_audio_header(unsigned short int psize)
file->Write(streamID, sizeof(streamID));
- psize +=
- sizeof(stuffing_bytes) +
- sizeof(std_buffer) +
- sizeof(PTS);
+ psize += sizeof(stuffing_bytes) + sizeof(std_buffer) + sizeof(PTS);
packetsize[0] = ((char*)&psize)[1];
packetsize[1] = ((char*)&psize)[0];
file->Write(packetsize, sizeof(packetsize));
@@ -299,3 +383,4 @@ void MovEncoderWriter::write_audio_header(unsigned short int psize)
}
+#endif /*0*/