diff --git a/accessunit.c b/accessunit.c index 69982e3..b9c1278 100644 --- a/accessunit.c +++ b/accessunit.c @@ -105,36 +105,35 @@ extern void free_access_unit(access_unit_p *acc_unit) } /* - * Report on this access unit, on the given output stream. + * Report on this access unit */ -extern void report_access_unit(FILE *stream, - access_unit_p access_unit) +extern void report_access_unit(access_unit_p access_unit) { int ii; - fprintf(stream,"Access unit %u",access_unit->index); + fprint_msg("Access unit %u",access_unit->index); if (access_unit->started_primary_picture) - fprintf(stream," (%s)",access_unit->primary_start->start_reason); - fprintf(stream,":\n"); + fprint_msg(" (%s)",access_unit->primary_start->start_reason); + print_msg(":\n"); if (access_unit->field_pic_flag) - fprintf(stream," %s field of frame %u\n", - (access_unit->bottom_field_flag==1?"Bottom":"Top"), - access_unit->frame_num); + fprint_msg(" %s field of frame %u\n", + (access_unit->bottom_field_flag==1?"Bottom":"Top"), + access_unit->frame_num); else - fprintf(stream," Frame %u\n",access_unit->frame_num); + fprint_msg(" Frame %u\n",access_unit->frame_num); if (access_unit->ignored_broken_NAL_units) - fprintf(stream," Ignored %d broken NAL unit%s\n", - access_unit->ignored_broken_NAL_units, - (access_unit->ignored_broken_NAL_units==1?"":"s")); + fprint_msg(" Ignored %d broken NAL unit%s\n", + access_unit->ignored_broken_NAL_units, + (access_unit->ignored_broken_NAL_units==1?"":"s")); for (ii=0; iinal_units->length; ii++) { nal_unit_p nal = access_unit->nal_units->array[ii]; if (nal == NULL) - fprintf(stream," \n"); + print_msg(" \n"); else { - fprintf(stream," %c",((access_unit->primary_start == nal)?'*':' ')); + fprint_msg(" %c",((access_unit->primary_start == nal)?'*':' ')); report_nal(TRUE,nal); } } @@ -672,20 +671,20 @@ static inline int end_access_unit(access_unit_context_p context, { if (show_details) { - report_access_unit(stdout,access_unit); + report_access_unit(access_unit); if (context->pending_nal) { - printf("... pending: "); + print_msg("... pending: "); report_nal(TRUE,context->pending_nal); } if (context->end_of_sequence) { - printf("--> EndOfSequence "); + print_msg("--> EndOfSequence "); report_nal(TRUE,context->end_of_sequence); } if (context->end_of_stream) { - printf("--> EndOfStream "); + print_msg("--> EndOfStream "); report_nal(TRUE,context->end_of_stream); } } @@ -904,9 +903,9 @@ static int maybe_remember_access_unit(reverse_data_p reverse_data, access_unit->index); return 1; } - if (verbose) printf("REMEMBER IDR %5d at " OFFSET_T_FORMAT_08 - "/%04d for %5d\n",access_unit->index, - start_posn.infile,start_posn.inpacket,num_bytes); + if (verbose) fprint_msg("REMEMBER IDR %5d at " OFFSET_T_FORMAT_08 + "/%04d for %5d\n",access_unit->index, + start_posn.infile,start_posn.inpacket,num_bytes); } return 0; } @@ -1056,7 +1055,7 @@ extern int get_next_access_unit(access_unit_context_p context, else if (nal_is_redundant(nal)) { // pass - // printf(" ignoring redundant NAL unit\n"); + // print_msg(" ignoring redundant NAL unit\n"); free_nal_unit(&nal); } else @@ -1270,8 +1269,8 @@ static int get_next_field_of_pair(access_unit_context_p context, access_unit_p second; if (show_details || context->nac->show_nal_details) - printf("@@ Looking for second field (%s time)\n", - (first_time?"first":"second")); + fprint_msg("@@ Looking for second field (%s time)\n", + (first_time?"first":"second")); // We assume (hope) the next picture will be our second half err = get_next_non_empty_access_unit(context,quiet,show_details,&second); @@ -1294,7 +1293,7 @@ static int get_next_field_of_pair(access_unit_context_p context, { // They appear to be matching fields - make a frame from them if (show_details || context->nac->show_nal_details) - printf("@@ Merging two field access units\n"); + print_msg("@@ Merging two field access units\n"); err = merge_access_unit_nals(*access_unit,&second); // (frees `second`) if (err) { @@ -1302,7 +1301,7 @@ static int get_next_field_of_pair(access_unit_context_p context, return 1; } if (show_details) - report_access_unit(stdout,*access_unit); + report_access_unit(*access_unit); } else if (first_time) { diff --git a/accessunit_fns.h b/accessunit_fns.h index 2bff871..0af6029 100644 --- a/accessunit_fns.h +++ b/accessunit_fns.h @@ -79,10 +79,9 @@ extern int rewind_access_unit_context(access_unit_context_p context); extern void free_access_unit(access_unit_p *acc_unit); /* - * Report on this access unit, on the given output stream. + * Report on this access unit */ -extern void report_access_unit(FILE *stream, - access_unit_p access_unit); +extern void report_access_unit(access_unit_p access_unit); /* * Retrieve the bounds of this access unit in the file it was read from. diff --git a/esreport.c b/esreport.c index 3ec3a63..28357f3 100644 --- a/esreport.c +++ b/esreport.c @@ -748,7 +748,7 @@ static void find_h264_fields(ES_p es, if (access_unit->field_pic_flag == 1) { - report_access_unit(stdout,access_unit); + report_access_unit(access_unit); num_fields ++; } else @@ -823,7 +823,7 @@ static void report_h264_frames(ES_p es, break; if (!quiet) - report_access_unit(stdout,access_unit); + report_access_unit(access_unit); if (count_sizes) { diff --git a/pes.c b/pes.c index 2c1ae49..122c5cd 100644 --- a/pes.c +++ b/pes.c @@ -39,6 +39,7 @@ #include "pidint_fns.h" #include "h262_fns.h" #include "tswrite_fns.h" +#include "printing_fns.h" #include "misc_fns.h" @@ -83,7 +84,7 @@ static int build_PES_packet_data(PES_packet_data_p *data) PES_packet_data_p new = malloc(SIZEOF_PES_PACKET_DATA); if (new == NULL) { - fprintf(stderr,"### Unable to allocate PES packet datastructure\n"); + print_err("### Unable to allocate PES packet datastructure\n"); return 1; } @@ -118,7 +119,7 @@ static inline int extend_PES_packet_data(PES_packet_data_p data, data->data = malloc(bytes_len); if (data->data == NULL) { - fprintf(stderr,"### Unable to extend PES packet data array\n"); + print_err("### Unable to extend PES packet data array\n"); return 1; } memcpy(data->data,bytes,bytes_len); @@ -129,7 +130,7 @@ static inline int extend_PES_packet_data(PES_packet_data_p data, data->data = realloc(data->data,data->data_len + bytes_len); if (data->data == NULL) { - fprintf(stderr,"### Unable to extend PES packet data array\n"); + print_err("### Unable to extend PES packet data array\n"); return 1; } memcpy(&(data->data[data->data_len]),bytes,bytes_len); @@ -157,7 +158,7 @@ static inline int build_dummy_PES_packet_data(PES_packet_data_p *data, err = build_PES_packet_data(&local_data); if (err) { - fprintf(stderr,"### Error building dummy PES packet\n"); + print_err("### Error building dummy PES packet\n"); return 1; } local_data->is_video = FALSE; @@ -167,7 +168,7 @@ static inline int build_dummy_PES_packet_data(PES_packet_data_p *data, local_data->data = malloc(data_len); if (local_data->data == NULL) { - fprintf(stderr,"### Unable to extend dummy PES packet data array\n"); + print_err("### Unable to extend dummy PES packet data array\n"); return 1; } memset(local_data->data,0xFF,data_len); @@ -177,7 +178,7 @@ static inline int build_dummy_PES_packet_data(PES_packet_data_p *data, local_data->data = realloc(local_data->data,data_len); if (local_data->data == NULL) { - fprintf(stderr,"### Unable to extend dummy PES packet data array\n"); + print_err("### Unable to extend dummy PES packet data array\n"); return 1; } memset(local_data->data,0xFF,data_len); @@ -244,14 +245,14 @@ static int init_peslist(peslist_p list) list->data = malloc(SIZEOF_PES_PACKET_DATA*PESLIST_START_SIZE); if (list->data == NULL) { - fprintf(stderr,"### Unable to allocate PES array in PID/PES data array"); + print_err("### Unable to allocate PES array in PID/PES data array"); return 1; } list->pid = malloc(sizeof(uint32_t)*PESLIST_START_SIZE); if (list->pid == NULL) { free(list->data); - fprintf(stderr,"### Unable to allocate PID array in PID/PES data array\n"); + print_err("### Unable to allocate PID array in PID/PES data array\n"); return 1; } // Just in case... @@ -270,7 +271,7 @@ static int build_peslist(peslist_p *list) peslist_p new = malloc(SIZEOF_PESLIST); if (new == NULL) { - fprintf(stderr,"### Unable to allocate PID/PES data array\n"); + print_err("### Unable to allocate PID/PES data array\n"); return 1; } @@ -368,15 +369,15 @@ static int start_packet_in_peslist(PES_reader_p reader, if (list == NULL) { - fprintf(stderr,"### Unable to append to NULL PID/PES data array\n"); + print_err("### Unable to append to NULL PID/PES data array\n"); return 1; } err = build_PES_packet_data(data); if (err) { - fprintf(stderr,"### Unable to build new PES packet datastructure" - " for PID/PES data array\n"); + print_err("### Unable to build new PES packet datastructure" + " for PID/PES data array\n"); return 1; } (*data)->is_video = is_video; @@ -390,11 +391,10 @@ static int start_packet_in_peslist(PES_reader_p reader, { PES_packet_data_p packet = list->data[ii]; if (reader->give_warning) - fprintf(stderr, - "!!! PID %04x (%d) already has an unfinished PES packet" - " associated with it\n %d byte%s of %d bytes were already" - " read - ignoring them\n",pid,pid,packet->data_len, - (packet->data_len==1?"":"s"),packet->length); + fprint_err("!!! PID %04x (%d) already has an unfinished PES packet" + " associated with it\n %d byte%s of %d bytes were already" + " read - ignoring them\n",pid,pid,packet->data_len, + (packet->data_len==1?"":"s"),packet->length); free_PES_packet_data(&(list->data[ii])); } list->data[ii] = *data; @@ -409,14 +409,14 @@ static int start_packet_in_peslist(PES_reader_p reader, list->data = realloc(list->data,newsize*SIZEOF_PES_PACKET_DATA); if (list->data == NULL) { - fprintf(stderr,"### Unable to extend PID/PES data array\n"); + print_err("### Unable to extend PID/PES data array\n"); free_PES_packet_data(data); return 1; } list->pid = realloc(list->pid,newsize*sizeof(uint32_t)); if (list->pid == NULL) { - fprintf(stderr,"### Unable to extend PID/PES data array\n"); + print_err("### Unable to extend PID/PES data array\n"); free_PES_packet_data(data); return 1; } @@ -461,15 +461,15 @@ static int clear_packet_in_peslist(peslist_p list, if (list == NULL) { - printf("Unable to clear PES packet in NULL PID/PES data array\n"); + print_msg("Unable to clear PES packet in NULL PID/PES data array\n"); return 1; } index = pid_index_in_peslist(list,pid); if (index == -1) { - fprintf(stderr,"### Unable to find PID %04x (%x) in PID/PES data array," - " so cannot clear its data\n",pid,pid); + fprint_err("### Unable to find PID %04x (%x) in PID/PES data array," + " so cannot clear its data\n",pid,pid); return 1; } list->data[index] = NULL; @@ -519,16 +519,15 @@ static int read_next_PES_packet_from_PS(PES_reader_p reader, err = read_PS_pack_header_body(reader->psreader,&header); if (err == EOF) { - fprintf(stderr,"!!! Unexpected EOF - partial PS packet at " - OFFSET_T_FORMAT " ignored\n",reader->posn); + fprint_err("!!! Unexpected EOF - partial PS packet at " + OFFSET_T_FORMAT " ignored\n",reader->posn); *packet_data = NULL; return EOF; } else if (err) { - fprintf(stderr, - "### Error reading data for pack header starting at " - OFFSET_T_FORMAT "\n",reader->posn); + fprint_err("### Error reading data for pack header starting at " + OFFSET_T_FORMAT "\n",reader->posn); return 1; } continue; @@ -537,15 +536,15 @@ static int read_next_PES_packet_from_PS(PES_reader_p reader, err = read_PS_packet_body(reader->psreader,stream_id,&packet); if (err == EOF) { - fprintf(stderr,"!!! Unexpected EOF - partial PS packet at " - OFFSET_T_FORMAT " ignored\n",reader->posn); + fprint_err("!!! Unexpected EOF - partial PS packet at " + OFFSET_T_FORMAT " ignored\n",reader->posn); *packet_data = NULL; return EOF; } else if (err) { - fprintf(stderr,"### Error reading PS packet starting at " - OFFSET_T_FORMAT "\n",reader->posn); + fprint_err("### Error reading PS packet starting at " + OFFSET_T_FORMAT "\n",reader->posn); return 1; } @@ -591,7 +590,7 @@ static int read_next_PES_packet_from_PS(PES_reader_p reader, keep = TRUE; is_video = FALSE; if (reader->give_info) - printf("Selecting audio stream number %d\n",stream_id & 0x1F); + fprint_msg("Selecting audio stream number %d\n",stream_id & 0x1F); } } else if (stream_id >= 0xe0 && stream_id <= 0xef) @@ -654,9 +653,9 @@ static void decide_pids(PES_reader_p reader) if (had_video) { if (reader->give_warning) - fprintf(stderr,"!!! Multiple video streams in TS program %d, PMT" - " at " OFFSET_T_FORMAT " - using PID %04x\n", - reader->program_number,reader->posn,reader->video_pid); + fprint_err("!!! Multiple video streams in TS program %d, PMT" + " at " OFFSET_T_FORMAT " - using PID %04x\n", + reader->program_number,reader->posn,reader->video_pid); break; } else if (reader->video_pid == 0) @@ -683,14 +682,14 @@ static void decide_pids(PES_reader_p reader) break; } if (reader->give_info) - printf(" Chose video PID %04x\n",reader->video_pid); + fprint_msg(" Chose video PID %04x\n",reader->video_pid); } else if (pmt->streams[ii].elementary_PID != reader->video_pid) { if (reader->give_warning) - fprintf(stderr,"!!! Video streams altered in TS program %d, PMT" - " at " OFFSET_T_FORMAT " - still using PID %04x\n", - reader->program_number,reader->posn,reader->video_pid); + fprint_err("!!! Video streams altered in TS program %d, PMT" + " at " OFFSET_T_FORMAT " - still using PID %04x\n", + reader->program_number,reader->posn,reader->video_pid); break; } had_video = TRUE; @@ -700,7 +699,7 @@ static void decide_pids(PES_reader_p reader) if (reader->video_only) { if (reader->give_info) - printf(" Not interested in any audio streams\n"); + print_msg(" Not interested in any audio streams\n"); return; } @@ -713,16 +712,16 @@ static void decide_pids(PES_reader_p reader) if (had_audio) { if (reader->give_warning) - fprintf(stderr,"!!! Multiple audio streams in TS program %d, PMT" - " at " OFFSET_T_FORMAT " - using PID %04x\n", - reader->program_number,reader->posn,reader->audio_pid); + fprint_err("!!! Multiple audio streams in TS program %d, PMT" + " at " OFFSET_T_FORMAT " - using PID %04x\n", + reader->program_number,reader->posn,reader->audio_pid); break; } else if (reader->audio_pid == 0) { reader->audio_pid = pmt->streams[ii].elementary_PID; if (reader->give_info) - printf(" Chose audio PID %04x\n",reader->audio_pid); + fprint_msg(" Chose audio PID %04x\n",reader->audio_pid); if (IS_DOLBY_STREAM_TYPE(pmt->streams[ii].stream_type)) { // Remember what stream type this Dolby data is using @@ -737,9 +736,9 @@ static void decide_pids(PES_reader_p reader) else if (pmt->streams[ii].elementary_PID != reader->audio_pid) { if (reader->give_warning) - fprintf(stderr,"!!! Audio streams altered in TS program %d, PMT" - " at " OFFSET_T_FORMAT " - still using PID %04x\n", - reader->program_number,reader->posn,reader->audio_pid); + fprint_err("!!! Audio streams altered in TS program %d, PMT" + " at " OFFSET_T_FORMAT " - still using PID %04x\n", + reader->program_number,reader->posn,reader->audio_pid); break; } had_audio = TRUE; @@ -775,13 +774,13 @@ static int refine_TS_program_info(PES_reader_p reader, #if DEBUG_PROGRAM_INFO if (reader->give_info) { - printf("PMT packet at " OFFSET_T_FORMAT ": first PMT, used as-is\n", - reader->posn); - report_pmt(stdout," ",reader->program_map); + fprint_msg("PMT packet at " OFFSET_T_FORMAT ": first PMT, used as-is\n", + reader->posn); + report_pmt(TRUE," ",reader->program_map); } #else if (reader->give_info) - report_pmt(stdout," ",reader->program_map); + report_pmt(TRUE," ",reader->program_map); #endif // And use its information to determine our video/audio PIDs decide_pids(reader); @@ -794,7 +793,7 @@ static int refine_TS_program_info(PES_reader_p reader, if (pmt->program_number != reader->program_number) { if (reader->give_info) - printf("Ignoring PMT for program %d\n",pmt->program_number); + fprint_msg("Ignoring PMT for program %d\n",pmt->program_number); free_pmt(&pmt); // since our caller will not free it return 0; } @@ -808,18 +807,18 @@ static int refine_TS_program_info(PES_reader_p reader, // Grumble or replace? Maybe both is safest if (reader->give_warning) { - fprintf(stderr,"!!! PMT in TS packet at " OFFSET_T_FORMAT - " replaces previous program information\n",reader->posn); - fprintf(stderr," Program information was:\n"); - report_pmt(stderr," ",reader->program_map); - fprintf(stderr," New program information is:\n"); - report_pmt(stderr," ",pmt); + fprint_err("!!! PMT in TS packet at " OFFSET_T_FORMAT + " replaces previous program information\n",reader->posn); + print_err(" Program information was:\n"); + report_pmt(FALSE," ",reader->program_map); + print_err(" New program information is:\n"); + report_pmt(FALSE," ",pmt); } else if (reader->give_info) { #if DEBUG_PROGRAM_INFO - printf("PMT packet at " OFFSET_T_FORMAT ": updating program info\n", - reader->posn); + fprint_msg("PMT packet at " OFFSET_T_FORMAT ": updating program info\n", + reader->posn); #endif report_pmt(stdout," ",pmt); } @@ -856,7 +855,7 @@ static int extract_and_refine_TS_program_info(PES_reader_p reader, err = extract_pmt(FALSE,pmt_data,pmt_data_len,pmt_pid,&pmt); if (err) { - fprintf(stderr,"### Error extracting stream list from PMT\n"); + print_err("### Error extracting stream list from PMT\n"); return 1; } @@ -865,8 +864,8 @@ static int extract_and_refine_TS_program_info(PES_reader_p reader, { #if DEBUG_PROGRAM_INFO if (reader->give_info) - printf("PMT packet at " OFFSET_T_FORMAT ": program number %d (not %d)\n", - reader->posn,pmt->program_number,reader->program_number); + fprint_msg("PMT packet at " OFFSET_T_FORMAT ": program number %d (not %d)\n", + reader->posn,pmt->program_number,reader->program_number); #endif free_pmt(&pmt); return 0; @@ -875,7 +874,7 @@ static int extract_and_refine_TS_program_info(PES_reader_p reader, err = refine_TS_program_info(reader,pmt); if (err) { - fprintf(stderr,"### Error refining TS program information from PMT\n"); + print_err("### Error refining TS program information from PMT\n"); free_pmt(&pmt); return 1; } @@ -907,19 +906,19 @@ static int find_first_PAT(PES_reader_p reader) &num_read,&prog_list); if (err) { - fprintf(stderr,"### Error finding first PAT\n"); + print_err("### Error finding first PAT\n"); return 1; } if (prog_list->length == 0) { - fprintf(stderr,"### No programs defined in first PAT (at " OFFSET_T_FORMAT - ")\n",reader->tsreader->posn - TS_PACKET_SIZE); + fprint_err("### No programs defined in first PAT (at " OFFSET_T_FORMAT + ")\n",reader->tsreader->posn - TS_PACKET_SIZE); free_pidint_list(&prog_list); return 1; } else if (prog_list->length > 1 && reader->give_info) - printf("Multiple programs in PAT - using the first\n\n"); + print_msg("Multiple programs in PAT - using the first\n\n"); if (reader->program_number == 0) { @@ -941,9 +940,9 @@ static int find_first_PAT(PES_reader_p reader) } if (!got_program) { - fprintf(stderr,"### Program %d not found in first PAT at " - OFFSET_T_FORMAT "\n",reader->program_number, - reader->tsreader->posn - TS_PACKET_SIZE); + fprint_err("### Program %d not found in first PAT at " + OFFSET_T_FORMAT "\n",reader->program_number, + reader->tsreader->posn - TS_PACKET_SIZE); return 1; } } @@ -972,8 +971,8 @@ static int find_first_PMT(PES_reader_p reader) FALSE,!reader->give_info,&nread,&pmt); if (err) { - fprintf(stderr,"### Error looking for program %d PMT with PID %04x" - " after first PAT\n",reader->program_number,reader->pmt_pid); + fprint_err("### Error looking for program %d PMT with PID %04x" + " after first PAT\n",reader->program_number,reader->pmt_pid); return 1; } @@ -981,8 +980,8 @@ static int find_first_PMT(PES_reader_p reader) break; if (reader->give_info) - printf("(Program is %d, not %d - ignoring it)\n", - pmt->program_number,reader->program_number); + fprint_msg("(Program is %d, not %d - ignoring it)\n", + pmt->program_number,reader->program_number); free_pmt(&pmt); } @@ -990,7 +989,7 @@ static int find_first_PMT(PES_reader_p reader) err = refine_TS_program_info(reader,pmt); if (err) { - fprintf(stderr,"### Error refining TS program information from PMT\n"); + print_err("### Error refining TS program information from PMT\n"); free_pmt(&pmt); return 1; } @@ -1029,20 +1028,20 @@ static int determine_TS_program_info(PES_reader_p reader) err = find_first_PAT(reader); if (err) { - fprintf(stderr,"### Error finding TS program information\n"); + print_err("### Error finding TS program information\n"); return 1; } err = find_first_PMT(reader); if (err) { - fprintf(stderr,"### Error finding TS program information\n"); + print_err("### Error finding TS program information\n"); return 1; } err = seek_using_TS_reader(reader->tsreader,0); if (err) { - fprintf(stderr,"### Error rewinding TS stream after finding initial" - " program information\n"); + print_err("### Error rewinding TS stream after finding initial" + " program information\n"); return 1; } // Having rewound, we mustn't forget to reset our notion of the TS packet @@ -1075,10 +1074,10 @@ static int start_new_PES_packet(PES_reader_p reader, PES_packet_data_p just_ended = NULL; PES_packet_data_p data; - //printf("%c",(pid==reader->video_pid?'V':'A'));fflush(stdout); + //fprint_msg("%c",(pid==reader->video_pid?'V':'A'));fflush(stdout); #if DEBUG_PES_ASSEMBLY - printf(": start new %s PES packet, payload_len = %d\n", - (pid==reader->video_pid?"video":"audio"),payload_len); + fprint_msg(": start new %s PES packet, payload_len = %d\n", + (pid==reader->video_pid?"video":"audio"),payload_len); print_data(TRUE,"Data",payload,payload_len,payload_len); #endif @@ -1091,18 +1090,16 @@ static int start_new_PES_packet(PES_reader_p reader, // which leaves *lots* of space. So I shall assume that it is an // error if the first six bytes (at least) of PES data are not // in the first TS packet - fprintf(stderr, - "### Only first %d byte%s of PES packet in its first TS packet," - " packet at " OFFSET_T_FORMAT "\n",payload_len, - (payload_len==1?"":"s"),reader->posn); + fprint_err("### Only first %d byte%s of PES packet in its first TS packet," + " packet at " OFFSET_T_FORMAT "\n",payload_len, + (payload_len==1?"":"s"),reader->posn); return 1; } if (payload[0] != 0 || payload[1] != 0 || payload[2] != 1) { - fprintf(stderr, - "### PES data starting in TS packet at " OFFSET_T_FORMAT - " starts %02X %02X %02X, not 00 00 01\n", - reader->posn,payload[0],payload[1],payload[2]); + fprint_err("### PES data starting in TS packet at " OFFSET_T_FORMAT + " starts %02X %02X %02X, not 00 00 01\n", + reader->posn,payload[0],payload[1],payload[2]); return 1; } @@ -1116,7 +1113,7 @@ static int start_new_PES_packet(PES_reader_p reader, reader->packets->data[index]->length == 0) { #if DEBUG_PES_ASSEMBLY - printf("@@@ just ended previous packet (by implication)\n"); + print_msg("@@@ just ended previous packet (by implication)\n"); #endif just_ended = reader->packets->data[index]; reader->packets->data[index] = NULL; @@ -1124,27 +1121,27 @@ static int start_new_PES_packet(PES_reader_p reader, // Anyway, start a new PES packet for this TS packet's data #if DEBUG_PES_ASSEMBLY - printf("@@@ start packet in PES list\n"); + print_msg("@@@ start packet in PES list\n"); #endif err = start_packet_in_peslist(reader,pid,pid==reader->video_pid,&data); if (err) { - fprintf(stderr,"### Error trying to start a new PES packet," - " for TS packet " OFFSET_T_FORMAT "\n",reader->posn); + fprint_err("### Error trying to start a new PES packet," + " for TS packet " OFFSET_T_FORMAT "\n",reader->posn); return 1; } #if DEBUG_PES_ASSEMBLY - printf("@@@ extend packet - data_len was %d\n",data->data_len); + fprint_msg("@@@ extend packet - data_len was %d\n",data->data_len); #endif err = extend_PES_packet_data(data,payload,payload_len); if (err) { - fprintf(stderr,"### Error remembering data at start of PES packet\n"); + print_err("### Error remembering data at start of PES packet\n"); return 1; } #if DEBUG_PES_ASSEMBLY - printf("@@@ data_len is now %d\n",data->data_len); + fprint_msg("@@@ data_len is now %d\n",data->data_len); #endif data->length = ((payload[4] << 8) | payload[5]); @@ -1152,7 +1149,7 @@ static int start_new_PES_packet(PES_reader_p reader, data->length += 6; // correct to the actual packet length #if DEBUG_PES_ASSEMBLY else - printf("@@@ PES packet marked as length 0\n"); + print_msg("@@@ PES packet marked as length 0\n"); #endif data->posn = reader->posn; @@ -1161,8 +1158,8 @@ static int start_new_PES_packet(PES_reader_p reader, { #if ALLOW_OVERLONG_PACKETS int extra; - fprintf(stderr,"### Found %d bytes of PES data, but expected %d" - " (PES packet length + 6)\n",data->data_len,data->length); + fprint_err("### Found %d bytes of PES data, but expected %d" + " (PES packet length + 6)\n",data->data_len,data->length); extra = data->data_len - data->length; if (extra > 0) { @@ -1170,10 +1167,10 @@ static int start_new_PES_packet(PES_reader_p reader, int from = payload_len - extra; print_data(FALSE," End of data",payload+from,extra,extra); #endif - fprintf(stderr," In %s PES packet, PID %x, starting at " - OFFSET_T_FORMAT "\n",(pid==reader->video_pid?"video":"audio"), - pid,reader->posn); - fprintf(stderr,"!!! Accepting packet anyway\n"); + fprint_err(" In %s PES packet, PID %x, starting at " + OFFSET_T_FORMAT "\n",(pid==reader->video_pid?"video":"audio"), + pid,reader->posn); + print_err("!!! Accepting packet anyway\n"); *finished = data; err = clear_packet_in_peslist(reader->packets,pid); if (err) return 1; @@ -1181,8 +1178,8 @@ static int start_new_PES_packet(PES_reader_p reader, else return 1; #else - fprintf(stderr,"### Found %d bytes of PES data, but expected %d" - " (PES packet length + 6)\n",data->data_len,data->length); + fprint_err("### Found %d bytes of PES data, but expected %d" + " (PES packet length + 6)\n",data->data_len,data->length); return 1; #endif } @@ -1202,7 +1199,7 @@ static int start_new_PES_packet(PES_reader_p reader, reader->deferred = *finished; // which might *not* be NULL *finished = just_ended; } - + return 0; } @@ -1229,27 +1226,27 @@ static int continue_PES_packet(PES_reader_p reader, PES_packet_data_p data; #if DEBUG_PES_ASSEMBLY - printf(": continue %s PES packet\n", - (pid==reader->video_pid?"video":"audio")); + fprint_msg(": continue %s PES packet\n", + (pid==reader->video_pid?"video":"audio")); #endif err = find_packet_in_peslist(reader->packets,pid,&data); if (err || data == NULL) { if (reader->give_warning) - fprintf(stderr,"!!! TS packet with PID %04x at " OFFSET_T_FORMAT - " continues an unstarted PES packet - ignoring it\n", - pid,reader->posn); + fprint_err("!!! TS packet with PID %04x at " OFFSET_T_FORMAT + " continues an unstarted PES packet - ignoring it\n", + pid,reader->posn); *finished = NULL; return 0; } - //printf("%c",(pid==reader->video_pid?'v':'a'));fflush(stdout); + //fprint_msg("%c",(pid==reader->video_pid?'v':'a'));fflush(stdout); err = extend_PES_packet_data(data,payload,payload_len); if (err) { - fprintf(stderr,"### Error remembering data to continue PES packet\n"); + print_err("### Error remembering data to continue PES packet\n"); return 1; } @@ -1257,8 +1254,8 @@ static int continue_PES_packet(PES_reader_p reader, { #if ALLOW_OVERLONG_PACKETS int extra; - fprintf(stderr,"### Found %d bytes of PES data, but expected %d" - " (PES packet length + 6)\n",data->data_len,data->length); + fprint_err("### Found %d bytes of PES data, but expected %d" + " (PES packet length + 6)\n",data->data_len,data->length); extra = data->data_len - data->length; if (extra > 0) { @@ -1266,10 +1263,10 @@ static int continue_PES_packet(PES_reader_p reader, int from = payload_len - extra; print_data(FALSE," End of data",payload+from,extra,extra); #endif - fprintf(stderr," In %s PES packet, PID %x, starting at " - OFFSET_T_FORMAT "\n",(pid==reader->video_pid?"video":"audio"), - pid,reader->posn); - fprintf(stderr,"!!! Accepting packet anyway\n"); + fprint_err(" In %s PES packet, PID %x, starting at " + OFFSET_T_FORMAT "\n",(pid==reader->video_pid?"video":"audio"), + pid,reader->posn); + print_err("!!! Accepting packet anyway\n"); *finished = data; err = clear_packet_in_peslist(reader->packets,pid); if (err) return 1; @@ -1277,8 +1274,8 @@ static int continue_PES_packet(PES_reader_p reader, else return 1; #else - fprintf(stderr,"### Found %d bytes of PES data, but expected %d" - " (PES packet length + 6)\n",data->data_len,data->length); + fprint_err("### Found %d bytes of PES data, but expected %d" + " (PES packet length + 6)\n",data->data_len,data->length); return 1; #endif } @@ -1351,7 +1348,7 @@ static int read_next_PES_packet_from_TS(PES_reader_p reader, *packet_data = reader->deferred; reader->deferred = NULL; #if DEBUG_PES_ASSEMBLY - printf("@@@ returning deferred PES packet\n"); + print_msg("@@@ returning deferred PES packet\n"); #endif return 0; } @@ -1400,8 +1397,8 @@ static int read_next_PES_packet_from_TS(PES_reader_p reader, } else if (err) { - fprintf(stderr,"### Error reading TS packet at " OFFSET_T_FORMAT "\n", - reader->posn); + fprint_err("### Error reading TS packet at " OFFSET_T_FORMAT "\n", + reader->posn); return 1; } @@ -1409,14 +1406,14 @@ static int read_next_PES_packet_from_TS(PES_reader_p reader, &adapt,&adapt_len,&payload,&payload_len); if (err) { - fprintf(stderr,"### Error interpreting TS packet at " OFFSET_T_FORMAT "\n", - reader->posn); + fprint_err("### Error interpreting TS packet at " OFFSET_T_FORMAT "\n", + reader->posn); return 1; } #if DEBUG_PES_ASSEMBLY - printf("@@@ TS packet at " OFFSET_T_FORMAT " with pid %3x", - reader->posn,pid); + fprint_msg("@@@ TS packet at " OFFSET_T_FORMAT " with pid %3x", + reader->posn,pid); #endif // If we're writing out TS packets directly to a client, then this @@ -1427,8 +1424,8 @@ static int read_next_PES_packet_from_TS(PES_reader_p reader, err = tswrite_write(reader->tswriter,ts_packet,pid,FALSE,0); if (err) { - fprintf(stderr,"### Error writing TS packet (PID %04x) at " - OFFSET_T_FORMAT "\n",pid,reader->posn); + fprint_err("### Error writing TS packet (PID %04x) at " + OFFSET_T_FORMAT "\n",pid,reader->posn); return 1; } } @@ -1438,21 +1435,21 @@ static int read_next_PES_packet_from_TS(PES_reader_p reader, // XXX We should probably check that the PAT for our program // has not changed... #if DEBUG_PES_ASSEMBLY - printf(": PAT\n"); + print_msg(": PAT\n"); #endif } else if (pid == reader->pmt_pid) { #if DEBUG_PES_ASSEMBLY - printf(": PMT\n"); + print_msg(": PMT\n"); #endif if (payload_unit_start_indicator && reader->pmt_data) { // This is the start of a new PMT packet, but we'd already // started one, so throw its data away - fprintf(stderr,"!!! Discarding previous (uncompleted) PMT data at " - OFFSET_T_FORMAT "\n",reader->posn); + fprint_err("!!! Discarding previous (uncompleted) PMT data at " + OFFSET_T_FORMAT "\n",reader->posn); free(reader->pmt_data); reader->pmt_data = NULL; reader->pmt_data_len = reader->pmt_data_used = 0; } @@ -1460,8 +1457,8 @@ static int read_next_PES_packet_from_TS(PES_reader_p reader, { // This is the continuation of a PMT packet, but we hadn't // started one yet - fprintf(stderr,"!!! Discarding PMT continuation, no PMT started, at " - OFFSET_T_FORMAT "\n",reader->posn); + fprint_err("!!! Discarding PMT continuation, no PMT started, at " + OFFSET_T_FORMAT "\n",reader->posn); continue; } @@ -1471,9 +1468,9 @@ static int read_next_PES_packet_from_TS(PES_reader_p reader, &reader->pmt_data_used); if (err) { - fprintf(stderr,"### Error %s PMT at " OFFSET_T_FORMAT "\n", - (payload_unit_start_indicator?"starting new":"continuing"), - reader->posn); + fprint_err("### Error %s PMT at " OFFSET_T_FORMAT "\n", + (payload_unit_start_indicator?"starting new":"continuing"), + reader->posn); if (reader->pmt_data) free(reader->pmt_data); return 1; } @@ -1487,9 +1484,8 @@ static int read_next_PES_packet_from_TS(PES_reader_p reader, reader->pmt_data_len); if (err) { - fprintf(stderr, - "### Error updating program info from PMT" - " (TS packet at " OFFSET_T_FORMAT ")\n",reader->posn); + fprint_err("### Error updating program info from PMT" + " (TS packet at " OFFSET_T_FORMAT ")\n",reader->posn); if (reader->pmt_data) free(reader->pmt_data); return 1; } @@ -1521,17 +1517,17 @@ static int read_next_PES_packet_from_TS(PES_reader_p reader, &finished); if (err) { - fprintf(stderr,"### Error %s PES packet (PID %04x)" - " with TS packet at " OFFSET_T_FORMAT "\n", - (payload_unit_start_indicator?"starting":"continuing"), - pid,reader->posn); + fprint_err("### Error %s PES packet (PID %04x)" + " with TS packet at " OFFSET_T_FORMAT "\n", + (payload_unit_start_indicator?"starting":"continuing"), + pid,reader->posn); print_data(FALSE," Data",payload,payload_len,20); return 1; } if (finished) { #if DEBUG_PES_ASSEMBLY - printf("@@@ PES packet with pid %x finished\n",pid); + fprint_msg("@@@ PES packet with pid %x finished\n",pid); report_PES_data_array(" ",finished->data,finished->data_len,TRUE); #endif @@ -1546,7 +1542,7 @@ static int read_next_PES_packet_from_TS(PES_reader_p reader, else { #if DEBUG_PES_ASSEMBLY - printf("@@@ return it\n"); + print_msg("@@@ return it\n"); #endif *packet_data = finished; break; @@ -1555,7 +1551,7 @@ static int read_next_PES_packet_from_TS(PES_reader_p reader, } #if DEBUG_PES_ASSEMBLY else - printf("\n"); + print_msg("\n"); #endif } return 0; @@ -1593,7 +1589,7 @@ extern int determine_if_TS_file(int input, break; else if (err) { - fprintf(stderr,"### Error trying to check if file is TS\n"); + print_err("### Error trying to check if file is TS\n"); return 1; } if (buf[0] != 0x47) @@ -1605,7 +1601,7 @@ extern int determine_if_TS_file(int input, err = seek_file(input,0); if (err) { - fprintf(stderr,"### Error rewinding file after determining if it is TS\n"); + print_err("### Error rewinding file after determining if it is TS\n"); return 1; } return 0; @@ -1632,8 +1628,8 @@ static int determine_PES_video_type(PES_reader_p reader) err = build_elementary_stream_PES(reader,&es); if (err) { - fprintf(stderr,"### Error starting elementary stream before" - " working out if PS is H.262 or H.264\n"); + print_err("### Error starting elementary stream before" + " working out if PS is H.262 or H.264\n"); return 1; } @@ -1642,7 +1638,7 @@ static int determine_PES_video_type(PES_reader_p reader) err = decide_ES_video_type(es,FALSE,FALSE,&reader->video_type); if (err) { - fprintf(stderr,"### Error deciding on PS video type\n"); + print_err("### Error deciding on PS video type\n"); free_elementary_stream(&es); return 1; } @@ -1654,8 +1650,7 @@ static int determine_PES_video_type(PES_reader_p reader) err = rewind_program_stream(reader->psreader); if (err) { - fprintf(stderr, - "### Error rewinding PS stream after determining its type\n"); + print_err("### Error rewinding PS stream after determining its type\n"); return 1; } return 0; @@ -1680,7 +1675,7 @@ static int build_PES_reader_datastructure(int give_info, PES_reader_p new = malloc(SIZEOF_PES_READER); if (new == NULL) { - fprintf(stderr,"### Unable to allocate PES reader datastructure\n"); + print_err("### Unable to allocate PES reader datastructure\n"); return 1; } @@ -1728,7 +1723,7 @@ static int build_PES_reader_datastructure(int give_info, err = build_peslist(&new->packets); if (err) { - fprintf(stderr,"### Error building PES reader datastructure\n"); + print_err("### Error building PES reader datastructure\n"); free(new); return 1; } @@ -1768,7 +1763,7 @@ extern int build_PS_PES_reader(PS_reader_p ps, err = determine_PES_video_type(*reader); if (err) { - fprintf(stderr,"### Error determining PS stream type\n"); + print_err("### Error determining PS stream type\n"); (void) free_PES_reader(reader); return 1; } @@ -1812,7 +1807,7 @@ extern int build_TS_PES_reader(TS_reader_p tsreader, err = determine_TS_program_info(*reader); if (err) { - fprintf(stderr,"### Error determining/checking program number\n"); + print_err("### Error determining/checking program number\n"); (void) free_PES_reader(reader); return 1; } @@ -1850,14 +1845,14 @@ extern int build_PES_reader(int input, err = build_TS_reader(input,&tsreader); if (err) { - fprintf(stderr,"### Error building TS specific reader\n"); + print_err("### Error building TS specific reader\n"); return 1; } err = build_TS_PES_reader(tsreader,give_info,give_warnings,program_number, reader); if (err) { - fprintf(stderr,"### Error building TS specific reader\n"); + print_err("### Error building TS specific reader\n"); free_TS_reader(&tsreader); return 1; } @@ -1868,13 +1863,13 @@ extern int build_PES_reader(int input, err = build_PS_reader(input,!give_info,&ps); if (err) { - fprintf(stderr,"### Error building PS specific reader\n"); + print_err("### Error building PS specific reader\n"); return 1; } err = build_PS_PES_reader(ps,give_info,give_warnings,reader); if (err) { - fprintf(stderr,"### Error building PS specific reader\n"); + print_err("### Error building PS specific reader\n"); free_PS_reader(&ps); return 1; } @@ -1914,7 +1909,7 @@ extern int open_PES_reader_for_TS(char *filename, input = open_binary_file(filename,FALSE); if (input == -1) { - fprintf(stderr,"### Unable to open input TS file %s\n",filename); + fprint_err("### Unable to open input TS file %s\n",filename); return 1; } err = build_PES_reader(input,TRUE,give_info,give_warnings,program_number, @@ -1944,7 +1939,7 @@ extern int open_PES_reader_for_PS(char *filename, int input = open_binary_file(filename,FALSE); if (input == -1) { - fprintf(stderr,"### Unable to open input PS file %s\n",filename); + fprint_err("### Unable to open input PS file %s\n",filename); return 1; } return build_PES_reader(input,FALSE,give_info,give_warnings,0,reader); @@ -2001,7 +1996,7 @@ extern int open_PES_reader(char *filename, input = open_binary_file(filename,FALSE); if (input == -1) { - fprintf(stderr,"### Unable to open input file %s\n",filename); + fprint_err("### Unable to open input file %s\n",filename); return 1; } err = determine_if_TS_file(input,&is_TS); @@ -2052,8 +2047,8 @@ extern int set_PES_reader_audio_stream(PES_reader_p reader, { if (stream_number < 0 || stream_number > 0x1F) { - fprintf(stderr,"### Audio stream number %d is not in range 0-31\n", - stream_number); + fprint_err("### Audio stream number %d is not in range 0-31\n", + stream_number); return 1; } reader->audio_stream_id = 0xc0 | stream_number; @@ -2291,7 +2286,7 @@ extern int close_PES_reader(PES_reader_p *reader) if ((*reader)->tsreader != NULL) { err = close_TS_reader(&(*reader)->tsreader); - if (err) fprintf(stderr,"### Error closing TS reader\n"); + if (err) print_err("### Error closing TS reader\n"); } } else @@ -2299,7 +2294,7 @@ extern int close_PES_reader(PES_reader_p *reader) if ((*reader)->psreader != NULL) { err = close_PS_file(&(*reader)->psreader); - if (err) fprintf(stderr,"### Error closing PS reader\n"); + if (err) print_err("### Error closing PS reader\n"); } } @@ -2343,16 +2338,16 @@ extern int read_next_PES_packet(PES_reader_p reader) #if DEBUG_READ_PACKETS if (reader->debug_read_packets) { - printf("<packet->posn, - reader->packet->data_len); + fprint_msg("<packet->posn, + reader->packet->data_len); if (reader->packet->is_video) { - printf(" VIDEO eslen %d",reader->packet->es_data_len); + fprint_msg(" VIDEO eslen %d",reader->packet->es_data_len); if (reader->packet->data_alignment_indicator) - printf(" aligned"); + print_msg(" aligned"); } - printf(">>\n"); + print_msg(">>\n"); } #endif if (reader->packet->is_video) @@ -2371,7 +2366,7 @@ extern int read_next_PES_packet(PES_reader_p reader) pid,stream_id,FALSE,0,0); if (err) { - fprintf(stderr,"### Error writing out PES packet as TS\n"); + print_err("### Error writing out PES packet as TS\n"); return 1; } if (reader->pes_padding) @@ -2388,7 +2383,7 @@ extern int read_next_PES_packet(PES_reader_p reader) pid,STREAM_ID_PADDING_STREAM,FALSE,0,0); if (err) { - fprintf(stderr,"### Error writing out dummy PES packet as TS\n"); + print_err("### Error writing out dummy PES packet as TS\n"); return 1; } } @@ -2409,10 +2404,10 @@ extern int read_next_PES_packet(PES_reader_p reader) if (reader->debug_read_packets) { if (err==EOF) - printf("<>\n"); + print_msg("<>\n"); else if (!err) - printf("<>\n", - reader->packet->posn); + fprint_msg("<>\n", + reader->packet->posn); } #endif @@ -2455,8 +2450,8 @@ extern int calc_mpeg1_pes_offset(byte *data, int data_len) posn ++; else { - fprintf(stderr,"### MPEG-1 PES packet has 0x%1xX" - " instead of 0x40, 0x2X, 0x3X or 0x0F\n",(data[posn]&0xF0)>>4); + fprint_err("### MPEG-1 PES packet has 0x%1xX" + " instead of 0x40, 0x2X, 0x3X or 0x0F\n",(data[posn]&0xF0)>>4); posn ++; // what else can we do? } } @@ -2568,10 +2563,10 @@ extern int read_next_PES_ES_packet(PES_reader_p reader) #ifdef DEBUG if (reader->give_info) { - printf(".. PES packet at " OFFSET_T_FORMAT " is %x (", - reader->packet->posn,reader->packet->data[3]); + fprint_msg(".. PES packet at " OFFSET_T_FORMAT " is %x (", + reader->packet->posn,reader->packet->data[3]); print_stream_id(stdout,reader->packet->data[3]); - printf(")%s\n",(reader->packet->is_video?" VIDEO":"")); + fprint_msg(")%s\n",(reader->packet->is_video?" VIDEO":"")); } #endif @@ -2630,15 +2625,15 @@ extern int decode_pts_dts(byte data[], if (guard != required_guard) { - fprintf(stderr,"!!! Guard bits at start of %s data are %x, not %x\n", - what,guard,required_guard); + fprint_err("!!! Guard bits at start of %s data are %x, not %x\n", + what,guard,required_guard); } pts1 = (data0 & 0x0E) >> 1; marker = data0 & 0x01; if (marker != 1) { - fprintf(stderr,"### First %s marker is not 1",what); + fprint_err("### First %s marker is not 1",what); return 1; } @@ -2646,7 +2641,7 @@ extern int decode_pts_dts(byte data[], marker = data2 & 0x01; if (marker != 1) { - fprintf(stderr,"### Second %s marker is not 1",what); + fprint_err("### Second %s marker is not 1",what); return 1; } @@ -2654,7 +2649,7 @@ extern int decode_pts_dts(byte data[], marker = data4 & 0x01; if (marker != 1) { - fprintf(stderr,"### Third %s marker is not 1",what); + fprint_err("### Third %s marker is not 1",what); return 1; } @@ -2691,8 +2686,8 @@ extern void encode_pts_dts(byte data[], case 1: what = "DTS after PTS"; break; default: what = "PTS/DTS/???"; break; } - fprintf(stderr,"!!! value " LLU_FORMAT " for %s is more than " LLU_FORMAT - " - reduced to " LLU_FORMAT "\n",value,what,MAX_PTS_VALUE,temp); + fprint_err("!!! value " LLU_FORMAT " for %s is more than " LLU_FORMAT + " - reduced to " LLU_FORMAT "\n",value,what,MAX_PTS_VALUE,temp); value = temp; } @@ -2726,9 +2721,9 @@ extern int PES_packet_has_PTS(PES_packet_data_p packet) if (data[0] != 0 || data[1] != 0 || data[2] != 1) { - fprintf(stderr,"### PES_packet_has_PTS: " - "PES packet start code prefix is %02x %02x %02x, not 00 00 01", - data[0],data[1],data[2]); + fprint_err("### PES_packet_has_PTS: " + "PES packet start code prefix is %02x %02x %02x, not 00 00 01", + data[0],data[1],data[2]); return FALSE; } @@ -2818,9 +2813,8 @@ extern int report_PES_data_array(char *prefix, if (data[0] != 0 || data[1] != 0 || data[2] != 1) { - fprintf(stderr, - "### PES packet start code prefix is %02x %02x %02x, not 00 00 01", - data[0],data[1],data[2]); + fprint_err("### PES packet start code prefix is %02x %02x %02x, not 00 00 01", + data[0],data[1],data[2]); return 1; } @@ -2831,17 +2825,17 @@ extern int report_PES_data_array(char *prefix, // if (packet_length == 0) // Elementary video data of unspecified length // return 0; - printf("%sPES packet: stream id %02x (",prefix,stream_id); + fprint_msg("%sPES packet: stream id %02x (",prefix,stream_id); print_stream_id(stdout,stream_id); - printf("), packet length %d",packet_length); + fprint_msg("), packet length %d",packet_length); if (packet_length == 0) { packet_length = data_len - 6; - printf(" (actual length %d)",packet_length); + fprint_msg(" (actual length %d)",packet_length); } else if (packet_length != data_len - 6) { - printf(" (actual length %d)",data_len - 6); + fprint_msg(" (actual length %d)",data_len - 6); } switch (stream_id) @@ -2853,11 +2847,11 @@ extern int report_PES_data_array(char *prefix, case STREAM_ID_PROGRAM_STREAM_DIRECTORY: case STREAM_ID_DSMCC_STREAM: case STREAM_ID_H222_E_STREAM: - printf("\n Just data bytes\n"); + print_msg("\n Just data bytes\n"); print_data(TRUE," ",bytes,packet_length,20); return 0; // Just data bytes case STREAM_ID_PADDING_STREAM: - printf("\n"); + print_msg("\n"); return 0; // Just padding bytes default: break; // Some sort of data we might be interested in dissecting @@ -2879,7 +2873,7 @@ extern int report_PES_data_array(char *prefix, int PES_CRC_flag; int PES_extension_flag; int PES_header_data_length; - printf("\n"); + print_msg("\n"); PES_scrambling_control = (bytes[0] & 0x30) >> 4; PES_priority = (bytes[0] & 0x08) >> 3; @@ -2887,13 +2881,13 @@ extern int report_PES_data_array(char *prefix, copyright = (bytes[0] & 0x02) >> 1; original_or_copy = bytes[0] & 0x01; - printf("%s scrambling %d, priority %d, data %s, %s, %s\n", - prefix, - PES_scrambling_control, - PES_priority, - (data_alignment_indicator?"aligned":"unaligned"), - (copyright?"copyrighted":"copyright undefined"), - (original_or_copy?"original":"copy")); + fprint_msg("%s scrambling %d, priority %d, data %s, %s, %s\n", + prefix, + PES_scrambling_control, + PES_priority, + (data_alignment_indicator?"aligned":"unaligned"), + (copyright?"copyrighted":"copyright undefined"), + (original_or_copy?"original":"copy")); PTS_DTS_flags = (bytes[1] & 0xC0) >> 6; ESCR_flag = (bytes[1] & 0x20) >> 5; @@ -2903,18 +2897,18 @@ extern int report_PES_data_array(char *prefix, PES_CRC_flag = (bytes[1] & 0x02) >> 1; PES_extension_flag = bytes[1] & 0x01; - printf("%s %s, ESCR %d, ES_rate %d, DSM trick mode %d, additional copy" - " info %d, PES CRC %d, PES extension %d\n", - prefix, - (PTS_DTS_flags==2?"PTS": - PTS_DTS_flags==3?"PTS & DTS": - PTS_DTS_flags==0?"no PTS/DTS":""), - ESCR_flag,ES_rate_flag,DSM_trick_mode_flag, - additional_copy_info_flag,PES_CRC_flag,PES_extension_flag); + fprint_msg("%s %s, ESCR %d, ES_rate %d, DSM trick mode %d, additional copy" + " info %d, PES CRC %d, PES extension %d\n", + prefix, + (PTS_DTS_flags==2?"PTS": + PTS_DTS_flags==3?"PTS & DTS": + PTS_DTS_flags==0?"no PTS/DTS":""), + ESCR_flag,ES_rate_flag,DSM_trick_mode_flag, + additional_copy_info_flag,PES_CRC_flag,PES_extension_flag); PES_header_data_length = bytes[2]; - printf("%s PES header data length %d\n",prefix,PES_header_data_length); + fprint_msg("%s PES header data length %d\n",prefix,PES_header_data_length); if (PTS_DTS_flags == 2) { @@ -2933,17 +2927,17 @@ extern int report_PES_data_array(char *prefix, } if (got_pts || got_dts) { - printf("%s PTS " LLU_FORMAT,prefix,pts); + fprint_msg("%s PTS " LLU_FORMAT,prefix,pts); if (got_dts) - printf(", DTS " LLU_FORMAT,dts); - printf("\n"); + fprint_msg(", DTS " LLU_FORMAT,dts); + print_msg("\n"); } if (show_data) { bytes += 3 + PES_header_data_length; if (prefix && strlen(prefix) > 0) - printf("%s",prefix); + fprint_msg("%s",prefix); print_data(TRUE," ",bytes,packet_length-3-PES_header_data_length,20); } } @@ -2951,7 +2945,7 @@ extern int report_PES_data_array(char *prefix, { // We assume it's MPEG-1 int posn = 0; - printf(" (MPEG-1)\n"); + print_msg(" (MPEG-1)\n"); // Ignore any up-front padding bytes while (posn < packet_length && bytes[posn] == 0xFF) posn++; @@ -2984,23 +2978,23 @@ extern int report_PES_data_array(char *prefix, posn ++; else { - fprintf(stderr,"### MPEG-1 PES packet has 0x%1xX" - " instead of 0x40, 0x2X, 0x3X or 0x0F\n",(bytes[posn]&0xF0)>>4); + fprint_err("### MPEG-1 PES packet has 0x%1xX" + " instead of 0x40, 0x2X, 0x3X or 0x0F\n",(bytes[posn]&0xF0)>>4); posn ++; // what else can we do? } if (got_pts || got_dts) { - printf("%s PTS " LLU_FORMAT,prefix,pts); + fprint_msg("%s PTS " LLU_FORMAT,prefix,pts); if (got_dts) - printf(", DTS " LLU_FORMAT,dts); - printf("\n"); + fprint_msg(", DTS " LLU_FORMAT,dts); + print_msg("\n"); } if (show_data) { bytes += posn; if (prefix && strlen(prefix) > 0) - printf("%s",prefix); + fprint_msg("%s",prefix); print_data(TRUE," ",bytes,packet_length-posn,20); } } @@ -3041,25 +3035,25 @@ extern void report_PES_data_array2(int stream_type, if (payload_len == 0) { - printf(" Payload has length 0\n"); + print_msg(" Payload has length 0\n"); return; } else if (payload == NULL) { - printf(" Payload is NULL, but should be length %d\n",payload_len); + fprint_msg(" Payload is NULL, but should be length %d\n",payload_len); return; } stream_id = payload[3]; PES_packet_length = (payload[4] << 8) | payload[5]; - printf(" PES header\n"); - printf(" Start code: %02x %02x %02x\n", - payload[0],payload[1],payload[2]); - printf(" Stream ID: %02x (%d) ",stream_id,stream_id); + print_msg(" PES header\n"); + fprint_msg(" Start code: %02x %02x %02x\n", + payload[0],payload[1],payload[2]); + fprint_msg(" Stream ID: %02x (%d) ",stream_id,stream_id); print_h262_start_code_str(stream_id); - printf("\n"); - printf(" PES packet length: %04x (%d)\n", - PES_packet_length,PES_packet_length); + print_msg("\n"); + fprint_msg(" PES packet length: %04x (%d)\n", + PES_packet_length,PES_packet_length); if (IS_H222_PES(payload)) { @@ -3073,60 +3067,60 @@ extern void report_PES_data_array2(int stream_type, case STREAM_ID_PROGRAM_STREAM_DIRECTORY: case STREAM_ID_DSMCC_STREAM: case STREAM_ID_H222_E_STREAM: - printf(" Just data bytes\n"); + print_msg(" Just data bytes\n"); print_data(TRUE," Data",payload+6,payload_len-6,1000); return; // Just data bytes case STREAM_ID_PADDING_STREAM: - printf(" Padding stream\n"); + print_msg(" Padding stream\n"); return; // Just padding bytes default: break; // Some sort of data we might be interested in dissecting } - printf(" Flags: %02x %02x",payload[6],payload[7]); + fprint_msg(" Flags: %02x %02x",payload[6],payload[7]); if (payload[6] != 0) { int scramble = (payload[6] & 0x30) >> 8; - if (scramble != 0) printf(" scramble-control %d",scramble); - if (ON(payload[6],0x08)) printf(" PES-priority"); - if (ON(payload[6],0x04)) printf(" data-aligned"); - if (ON(payload[6],0x02)) printf(" copyright"); - if (ON(payload[6],0x01)) printf(" original/copy"); + if (scramble != 0) fprint_msg(" scramble-control %d",scramble); + if (ON(payload[6],0x08)) print_msg(" PES-priority"); + if (ON(payload[6],0x04)) print_msg(" data-aligned"); + if (ON(payload[6],0x02)) print_msg(" copyright"); + if (ON(payload[6],0x01)) print_msg(" original/copy"); } if (payload[7] != 0) { - printf(" :"); + print_msg(" :"); if (ON(payload[7],0x80)) { with_pts = TRUE; - printf(" PTS"); + print_msg(" PTS"); } if (ON(payload[7],0x40)) { with_dts = TRUE; - printf(" DTS"); + print_msg(" DTS"); } - if (ON(payload[7],0x20)) printf(" ESCR"); - if (ON(payload[7],0x10)) printf(" ES-rate"); - if (ON(payload[7],0x08)) printf(" DSM-trick-mode"); - if (ON(payload[7],0x04)) printf(" more-copy-info"); - if (ON(payload[7],0x02)) printf(" CRC"); - if (ON(payload[7],0x01)) printf(" extension"); + if (ON(payload[7],0x20)) print_msg(" ESCR"); + if (ON(payload[7],0x10)) print_msg(" ES-rate"); + if (ON(payload[7],0x08)) print_msg(" DSM-trick-mode"); + if (ON(payload[7],0x04)) print_msg(" more-copy-info"); + if (ON(payload[7],0x02)) print_msg(" CRC"); + if (ON(payload[7],0x01)) print_msg(" extension"); } - printf("\n"); - printf(" PES header len %d\n", payload[8]); + print_msg("\n"); + fprint_msg(" PES header len %d\n", payload[8]); if (with_pts) { err = decode_pts_dts(&(payload[9]),(with_dts?3:2),&pts); if (!err) - printf(" PTS " LLU_FORMAT "\n",pts); + fprint_msg(" PTS " LLU_FORMAT "\n",pts); } if (with_dts) { err = decode_pts_dts(&(payload[14]),1,&dts); if (!err) - printf(" DTS " LLU_FORMAT "\n",dts); + fprint_msg(" DTS " LLU_FORMAT "\n",dts); } data = payload + 9 + payload[8]; @@ -3139,18 +3133,18 @@ extern void report_PES_data_array2(int stream_type, if (stream_type == 0x06 || stream_type == 0x81) { if (data_len >= 2 && data[0] == 0x0B && data[1] == 0x77) - printf(" AC-3 audio data\n"); + print_msg(" AC-3 audio data\n"); else if (data_len >= 4 && data[0] == 0x7F && data[1] == 0xFE && data[1] == 0x80 && data[2] == 0x01) - printf(" DTS audio data\n"); + print_msg(" DTS audio data\n"); } } else { // We assume it's MPEG-1 int posn = 0; - printf(" MPEG-1 packet layer packet\n"); + print_msg(" MPEG-1 packet layer packet\n"); if (stream_id != STREAM_ID_PRIVATE_STREAM_2) { @@ -3158,14 +3152,14 @@ extern void report_PES_data_array2(int stream_type, while (posn < PES_packet_length && payload[6+posn] == 0xFF) posn++; if (posn != 0) - printf(" %d stuffing byte%s\n",posn,posn==1?"":"s"); + fprint_msg(" %d stuffing byte%s\n",posn,posn==1?"":"s"); if (posn < PES_packet_length) { if ((payload[6+posn] & 0xC0) == 0x40) { - printf(" STD buffer scale %d\n",ON(payload[6+posn],5)); - printf(" STD buffer size %d\n",(payload[6+posn] & 0x1F) << 8 | + fprint_msg(" STD buffer scale %d\n",ON(payload[6+posn],5)); + fprint_msg(" STD buffer size %d\n",(payload[6+posn] & 0x1F) << 8 | (payload[6+posn+1])); posn += 2; } @@ -3194,16 +3188,16 @@ extern void report_PES_data_array2(int stream_type, posn ++; else { - fprintf(stderr,"### MPEG-1 PES packet has 0x%1xX" - " instead of 0x40, 0x2X, 0x3X or 0x0F\n",(payload[posn]&0xF0)>>4); + fprint_err("### MPEG-1 PES packet has 0x%1xX" + " instead of 0x40, 0x2X, 0x3X or 0x0F\n",(payload[posn]&0xF0)>>4); posn ++; // what else can we do? } if (with_pts || with_dts) { - printf(" PTS " LLU_FORMAT "\n",pts); + fprint_msg(" PTS " LLU_FORMAT "\n",pts); if (with_dts) - printf(" DTS " LLU_FORMAT "\n",dts); - printf("\n"); + fprint_msg(" DTS " LLU_FORMAT "\n",dts); + print_msg("\n"); } data = payload + 6 + posn; @@ -3245,8 +3239,8 @@ extern int find_PTS_in_PES(byte data[], if (data[0] != 0 || data[1] != 0 || data[2] != 1) { - fprintf(stderr,"### find_PTS_in_PES:" - " PES packet start code prefix is %02x %02x %02x, not 00 00 01\n", + fprint_err("### find_PTS_in_PES:" + " PES packet start code prefix is %02x %02x %02x, not 00 00 01\n", data[0],data[1],data[2]); return 1; } @@ -3338,9 +3332,9 @@ extern int find_DTS_in_PES(byte data[], if (data[0] != 0 || data[1] != 0 || data[2] != 1) { - fprintf(stderr,"### find_DTS_in_PES:" - " PES packet start code prefix is %02x %02x %02x, not 00 00 01\n", - data[0],data[1],data[2]); + fprint_err("### find_DTS_in_PES:" + " PES packet start code prefix is %02x %02x %02x, not 00 00 01\n", + data[0],data[1],data[2]); return 1; } @@ -3435,9 +3429,9 @@ extern int find_PTS_DTS_in_PES(byte data[], if (data[0] != 0 || data[1] != 0 || data[2] != 1) { - fprintf(stderr,"### find_PTS_in_PES:" - " PES packet start code prefix is %02x %02x %02x, not 00 00 01\n", - data[0],data[1],data[2]); + fprint_err("### find_PTS_in_PES:" + " PES packet start code prefix is %02x %02x %02x, not 00 00 01\n", + data[0],data[1],data[2]); return 1; } @@ -3547,9 +3541,9 @@ extern int find_ESCR_in_PES(byte data[], if (data[0] != 0 || data[1] != 0 || data[2] != 1) { - fprintf(stderr,"### find_ESCR_in_PES:" - " PES packet start code prefix is %02x %02x %02x, not 00 00 01\n", - data[0],data[1],data[2]); + fprint_err("### find_ESCR_in_PES:" + " PES packet start code prefix is %02x %02x %02x, not 00 00 01\n", + data[0],data[1],data[2]); return 1; } @@ -3738,8 +3732,8 @@ extern int write_program_data(PES_reader_p reader, reader->video_pid); if (stream == NULL) { - fprintf(stderr,"### Cannot find video PID %04x in program map\n", - reader->video_pid); + fprint_err("### Cannot find video PID %04x in program map\n", + reader->video_pid); return 1; } prog_pids[0] = reader->output_video_pid; // may not be the same @@ -3752,8 +3746,8 @@ extern int write_program_data(PES_reader_p reader, reader->audio_pid); if (stream == NULL) { - fprintf(stderr,"### Cannot find audio PID %04x in program map\n", - reader->audio_pid); + fprint_err("### Cannot find audio PID %04x in program map\n", + reader->audio_pid); return 1; } prog_pids[num_progs] = reader->output_audio_pid; // may not be the same @@ -3812,17 +3806,17 @@ extern int write_program_data(PES_reader_p reader, #if SHOW_PROGRAM_INFO if (reader->give_info) { - printf("PROGRAM %d: pmt %x (%d), pcr %x (%d)\n" - " video %x (%d) type %02x (%s)\n", - reader->output_program_number, - reader->output_pmt_pid,reader->output_pmt_pid, - pcr_pid,pcr_pid, - reader->output_video_pid,reader->output_video_pid, - prog_type[0],h222_stream_type_str(prog_type[0])); + fprint_msg("PROGRAM %d: pmt %x (%d), pcr %x (%d)\n" + " video %x (%d) type %02x (%s)\n", + reader->output_program_number, + reader->output_pmt_pid,reader->output_pmt_pid, + pcr_pid,pcr_pid, + reader->output_video_pid,reader->output_video_pid, + prog_type[0],h222_stream_type_str(prog_type[0])); if (num_progs == 2) - printf(" audio %x (%d) type %02x (%s)\n", - reader->output_audio_pid,reader->output_audio_pid, - prog_type[1],h222_stream_type_str(prog_type[1])); + fprint_msg(" audio %x (%d) type %02x (%s)\n", + reader->output_audio_pid,reader->output_audio_pid, + prog_type[1],h222_stream_type_str(prog_type[1])); } #endif @@ -3833,7 +3827,7 @@ extern int write_program_data(PES_reader_p reader, num_progs,prog_pids,prog_type); if (err) { - fprintf(stderr,"### Error writing out TS program data\n"); + print_err("### Error writing out TS program data\n"); return 1; } return 0; diff --git a/pidint.c b/pidint.c index 368ac7d..b1656d4 100644 --- a/pidint.c +++ b/pidint.c @@ -33,6 +33,7 @@ #include "compat.h" #include "pidint_fns.h" #include "misc_fns.h" +#include "printing_fns.h" #include "ts_fns.h" #include "h222_defns.h" @@ -49,14 +50,14 @@ extern int init_pidint_list(pidint_list_p list) list->number = malloc(sizeof(int)*PIDINT_LIST_START_SIZE); if (list->number == NULL) { - fprintf(stderr,"### Unable to allocate array in program list datastructure\n"); + print_err("### Unable to allocate array in program list datastructure\n"); return 1; } list->pid = malloc(sizeof(uint32_t)*PIDINT_LIST_START_SIZE); if (list->pid == NULL) { free(list->number); - fprintf(stderr,"### Unable to allocate array in program list datastructure\n"); + print_err("### Unable to allocate array in program list datastructure\n"); return 1; } return 0; @@ -73,7 +74,7 @@ extern int build_pidint_list(pidint_list_p *list) pidint_list_p new = malloc(SIZEOF_PIDINT_LIST); if (new == NULL) { - fprintf(stderr,"### Unable to allocate pid/int list datastructure\n"); + print_err("### Unable to allocate pid/int list datastructure\n"); return 1; } @@ -95,7 +96,7 @@ extern int append_to_pidint_list(pidint_list_p list, { if (list == NULL) { - fprintf(stderr,"### Unable to append to NULL pid/int list\n"); + print_err("### Unable to append to NULL pid/int list\n"); return 1; } @@ -105,13 +106,13 @@ extern int append_to_pidint_list(pidint_list_p list, list->number = realloc(list->number,newsize*sizeof(int)); if (list->number == NULL) { - fprintf(stderr,"### Unable to extend pid/int list array\n"); + print_err("### Unable to extend pid/int list array\n"); return 1; } list->pid = realloc(list->pid,newsize*sizeof(uint32_t)); if (list->pid == NULL) { - fprintf(stderr,"### Unable to extend pid/int list array\n"); + print_err("### Unable to extend pid/int list array\n"); return 1; } list->size = newsize; @@ -134,15 +135,15 @@ extern int remove_from_pidint_list(pidint_list_p list, int ii; if (list == NULL) { - fprintf(stderr,"### Unable to remove entry from NULL pid/int list\n"); + print_err("### Unable to remove entry from NULL pid/int list\n"); return 1; } index = pid_index_in_pidint_list(list,pid); if (index == -1) { - fprintf(stderr,"### Cannot remove PID %04x from pid/int list" - " - it is not there\n",pid); + fprint_err("### Cannot remove PID %04x from pid/int list" + " - it is not there\n",pid); return 1; } @@ -191,21 +192,21 @@ extern void report_pidint_list(pidint_list_p list, int pid_first) { if (list == NULL) - printf("%s is NULL\n",list_name); + fprint_msg("%s is NULL\n",list_name); else if (list->length == 0) - printf("%s is empty\n",list_name); + fprint_msg("%s is empty\n",list_name); else { int ii; - printf("%s:\n",list_name); + fprint_msg("%s:\n",list_name); for (ii=0; iilength; ii++) { if (pid_first) - printf(" PID %04x (%d) -> %s %d\n", - list->pid[ii],list->pid[ii],int_name,list->number[ii]); + fprint_msg(" PID %04x (%d) -> %s %d\n", + list->pid[ii],list->pid[ii],int_name,list->number[ii]); else - printf(" %s %d -> PID %04x (%d)\n", - int_name,list->number[ii],list->pid[ii],list->pid[ii]); + fprint_msg(" %s %d -> PID %04x (%d)\n", + int_name,list->number[ii],list->pid[ii],list->pid[ii]); } } } @@ -313,21 +314,21 @@ extern int same_pidint_list(pidint_list_p list1, extern void report_stream_list(pidint_list_p list, char *prefix) { - if (prefix!=NULL) printf(prefix); + if (prefix!=NULL) print_msg(prefix); if (list == NULL) - printf("Program stream list is NULL\n"); + print_msg("Program stream list is NULL\n"); else if (list->length == 0) - printf("Program stream list is empty\n"); + print_msg("Program stream list is empty\n"); else { int ii; - printf("Program streams:\n"); + print_msg("Program streams:\n"); for (ii=0; iilength; ii++) { - if (prefix!=NULL) printf(prefix); - printf(" PID %04x (%d) -> Stream type %3d (%s)\n", - list->pid[ii],list->pid[ii],list->number[ii], - h222_stream_type_str(list->number[ii])); + if (prefix!=NULL) print_msg(prefix); + fprint_msg(" PID %04x (%d) -> Stream type %3d (%s)\n", + list->pid[ii],list->pid[ii],list->number[ii], + h222_stream_type_str(list->number[ii])); } } } @@ -345,7 +346,7 @@ static int init_pmt_streams(pmt_p pmt) pmt->streams = malloc(SIZEOF_PMT_STREAM*PMT_STREAMS_START_SIZE); if (pmt->streams == NULL) { - fprintf(stderr,"### Unable to allocate streams in PMT datastructure\n"); + print_err("### Unable to allocate streams in PMT datastructure\n"); return 1; } return 0; @@ -378,16 +379,16 @@ extern pmt_p build_pmt(uint16_t program_number, byte version_number, if (PCR_pid != 0x1FFF && (PCR_pid < 0x0010 || PCR_pid > 0x1ffe)) { - fprintf(stderr,"### Error building PMT datastructure\n" - " PCR PID %04x is outside legal program stream range\n", - PCR_pid); + fprint_err("### Error building PMT datastructure\n" + " PCR PID %04x is outside legal program stream range\n", + PCR_pid); return NULL; } new = malloc(SIZEOF_PMT); if (new == NULL) { - fprintf(stderr,"### Unable to allocate PMT datastructure\n"); + print_err("### Unable to allocate PMT datastructure\n"); return NULL; } @@ -422,8 +423,8 @@ extern int set_pmt_program_info(pmt_p pmt, { if (program_info_length > PMT_MAX_INFO_LENGTH) { - fprintf(stderr,"### Program info length %d is more than %d\n", - program_info_length,PMT_MAX_INFO_LENGTH); + fprint_err("### Program info length %d is more than %d\n", + program_info_length,PMT_MAX_INFO_LENGTH); return 1; } if (pmt->program_info == NULL) @@ -431,7 +432,7 @@ extern int set_pmt_program_info(pmt_p pmt, pmt->program_info = malloc(program_info_length); if (pmt->program_info == NULL) { - fprintf(stderr,"### Unable to allocate program info in PMT datastructure\n"); + print_err("### Unable to allocate program info in PMT datastructure\n"); return 1; } } @@ -441,7 +442,7 @@ extern int set_pmt_program_info(pmt_p pmt, pmt->program_info = realloc(pmt->program_info,program_info_length); if (pmt->program_info == NULL) { - fprintf(stderr,"### Unable to extend program info in PMT datastructure\n"); + print_err("### Unable to extend program info in PMT datastructure\n"); return 1; } } @@ -465,22 +466,22 @@ extern int add_stream_to_pmt(pmt_p pmt, { if (pmt == NULL) { - fprintf(stderr,"### Unable to append to NULL PMT datastructure\n"); + print_err("### Unable to append to NULL PMT datastructure\n"); return 1; } if (elementary_PID < 0x0010 || elementary_PID > 0x1ffe) { - fprintf(stderr,"### Error adding stream to PMT\n" - " Elementary PID %04x is outside legal program stream range\n", - elementary_PID); + fprint_err("### Error adding stream to PMT\n" + " Elementary PID %04x is outside legal program stream range\n", + elementary_PID); return 1; } if (ES_info_length > PMT_MAX_INFO_LENGTH) { - fprintf(stderr,"### ES info length %d is more than %d\n", - ES_info_length,PMT_MAX_INFO_LENGTH); + fprint_err("### ES info length %d is more than %d\n", + ES_info_length,PMT_MAX_INFO_LENGTH); return 1; } @@ -490,7 +491,7 @@ extern int add_stream_to_pmt(pmt_p pmt, pmt->streams = realloc(pmt->streams,newsize*SIZEOF_PMT_STREAM); if (pmt->streams == NULL) { - fprintf(stderr,"### Unable to extend PMT streams array\n"); + print_err("### Unable to extend PMT streams array\n"); return 1; } pmt->streams_size = newsize; @@ -503,7 +504,7 @@ extern int add_stream_to_pmt(pmt_p pmt, pmt->streams[pmt->num_streams].ES_info = malloc(ES_info_length); if (pmt->streams[pmt->num_streams].ES_info == NULL) { - fprintf(stderr,"### Unable to allocate PMT stream ES info\n"); + print_err("### Unable to allocate PMT stream ES info\n"); return 1; } memcpy(pmt->streams[pmt->num_streams].ES_info,ES_info,ES_info_length); @@ -539,15 +540,15 @@ extern int remove_stream_from_pmt(pmt_p pmt, int ii; if (pmt == NULL) { - fprintf(stderr,"### Unable to remove entry from NULL PMT datastructure\n"); + print_err("### Unable to remove entry from NULL PMT datastructure\n"); return 1; } index = pid_index_in_pmt(pmt,pid); if (index == -1) { - fprintf(stderr,"### Cannot remove PID %04x from PMT datastructure" - " - it is not there\n",pid); + fprint_err("### Cannot remove PID %04x from PMT datastructure" + " - it is not there\n",pid); return 1; } @@ -713,54 +714,54 @@ extern int same_pmt(pmt_p pmt1, /* * Report on a PMT datastructure. * - * - `stream` is the stream to write to + * - if `is_msg`, report as a message, otherwise as an error * - `prefix` is NULL or a string to put before each line printed * - `pmt` is the PMT to report on */ -extern void report_pmt(FILE *stream, +extern void report_pmt(int is_msg, char *prefix, pmt_p pmt) { - if (prefix!=NULL) fprintf(stream,prefix); + if (prefix!=NULL) fprint_msg_or_err(is_msg,prefix); if (pmt == NULL) { - fprintf(stream,"PMT is NULL\n"); + fprint_msg_or_err(is_msg,"PMT is NULL\n"); return; } else - fprintf(stream,"Program %d, version %d, PCR PID %04x (%d)\n", - pmt->program_number,pmt->version_number,pmt->PCR_pid,pmt->PCR_pid); + fprint_msg_or_err(is_msg,"Program %d, version %d, PCR PID %04x (%d)\n", + pmt->program_number,pmt->version_number,pmt->PCR_pid,pmt->PCR_pid); if (pmt->program_info_length > 0) { - if (prefix!=NULL) fprintf(stream,prefix); - print_data(stream==stdout," Program info",pmt->program_info, + if (prefix!=NULL) fprint_msg_or_err(is_msg,prefix); + print_data(is_msg," Program info",pmt->program_info, pmt->program_info_length,pmt->program_info_length); - print_descriptors(stream,prefix," ",pmt->program_info, + print_descriptors(is_msg,prefix," ",pmt->program_info, pmt->program_info_length); } if (pmt->num_streams > 0) { int ii; - if (prefix!=NULL) fprintf(stream,prefix); - fprintf(stream,"Program streams:\n"); + if (prefix!=NULL) fprint_msg_or_err(is_msg,prefix); + fprint_msg_or_err(is_msg,"Program streams:\n"); for (ii=0; iinum_streams; ii++) { - if (prefix!=NULL) fprintf(stream,prefix); - fprintf(stream," PID %04x (%4d) -> Stream type %02x (%3d) %s\n", - pmt->streams[ii].elementary_PID, - pmt->streams[ii].elementary_PID, - pmt->streams[ii].stream_type, - pmt->streams[ii].stream_type, - h222_stream_type_str(pmt->streams[ii].stream_type)); + if (prefix!=NULL) fprint_msg_or_err(is_msg,prefix); + fprint_msg_or_err(is_msg," PID %04x (%4d) -> Stream type %02x (%3d) %s\n", + pmt->streams[ii].elementary_PID, + pmt->streams[ii].elementary_PID, + pmt->streams[ii].stream_type, + pmt->streams[ii].stream_type, + h222_stream_type_str(pmt->streams[ii].stream_type)); if (pmt->streams[ii].ES_info_length > 0) { - if (prefix!=NULL) fprintf(stream,prefix); - print_data(stream==stdout," ES info", + if (prefix!=NULL) fprint_msg_or_err(is_msg,prefix); + print_data(is_msg," ES info", pmt->streams[ii].ES_info, pmt->streams[ii].ES_info_length, pmt->streams[ii].ES_info_length); - print_descriptors(stream,prefix," ", + print_descriptors(is_msg,prefix," ", pmt->streams[ii].ES_info, pmt->streams[ii].ES_info_length); } diff --git a/pidint_fns.h b/pidint_fns.h index 4b8d253..2d32b95 100644 --- a/pidint_fns.h +++ b/pidint_fns.h @@ -229,11 +229,11 @@ extern int same_pmt(pmt_p pmt1, /* * Report on a PMT datastructure. * - * - `stream` is the stream to write to + * - if `is_msg`, report as a message, otherwise as an error * - `prefix` is NULL or a string to put before each line printed * - `pmt` is the PMT to report on */ -extern void report_pmt(FILE *stream, +extern void report_pmt(int is_msg, char *prefix, pmt_p pmt); diff --git a/ts.c b/ts.c index 903dcb0..f02bf5e 100644 --- a/ts.c +++ b/ts.c @@ -41,6 +41,7 @@ #include "ts_fns.h" #include "tswrite_fns.h" #include "misc_fns.h" +#include "printing_fns.h" #include "pidint_fns.h" #include "pes_fns.h" @@ -1005,7 +1006,7 @@ extern int write_pmt(TS_writer_p output, if (section_length > 1021) { fprintf(stderr,"### PMT data is too long - will not fit in 1021 bytes\n"); - report_pmt(stderr," ",pmt); + report_pmt(FALSE," ",pmt); return 1; } @@ -2193,7 +2194,7 @@ extern int extract_prog_list_from_pat(int verbose, * Print out information about program descriptors * (either from the PMT program info, or the PMT/stream ES info) * - * - `stream` is the stream to print on + * - if `is_msg` then print as a message, otherwise as an error * - `leader1` and `leader2` are the text to write at the start of each line * (either or both may be NULL) * - `desc_data` is the data containing the descriptors @@ -2203,7 +2204,7 @@ extern int extract_prog_list_from_pat(int verbose, * * If you want to interpret more descriptors then ITU-T J.94 is the standard */ -extern int print_descriptors(FILE *stream, +extern int print_descriptors(int is_msg, char *leader1, char *leader2, byte *desc_data, @@ -2224,8 +2225,8 @@ extern int print_descriptors(FILE *stream, if (this_length > data_len) { // Not much we can do - try giving up? - fprintf(stream,"Descriptor %x says length %d, but only %d bytes left\n", - tag,this_length,data_len); + fprint_msg_or_err(is_msg,"Descriptor %x says length %d, but only %d bytes left\n", + tag,this_length,data_len); return 1; // Hmm - well, maybe } @@ -2249,63 +2250,63 @@ extern int print_descriptors(FILE *stream, tag==18?"IBP": tag>19 && tag<64?"Reserved":NULL); - if (leader1 != NULL) fputs(leader1,stream); - if (leader2 != NULL) fputs(leader2,stream); + if (leader1 != NULL) fprint_msg_or_err(is_msg,"%s",leader1); + if (leader2 != NULL) fprint_msg_or_err(is_msg,"%s",leader2); if (name != NULL) - print_data(stream==stdout,name,data,this_length,100); + print_data(is_msg,name,data,this_length,100); else { switch (tag) { uint32_t temp_u; case 5: - fprintf(stream,"Registration "); + fprint_msg_or_err(is_msg,"Registration "); if (this_length >= 4) { for (ii=0; ii<4; ii++) { if (isprint(data[ii])) - putc(data[ii],stream); + fprint_msg_or_err(is_msg,"%c",data[ii]); else - fprintf(stream,"<%02x>",data[ii]); + fprint_msg_or_err(is_msg,"<%02x>",data[ii]); } if (this_length > 4) for (ii=4; ii < this_length; ii++) - fprintf(stream," %02x",data[ii]); + fprint_msg_or_err(is_msg," %02x",data[ii]); } - fprintf(stream,"\n"); + fprint_msg_or_err(is_msg,"\n"); break; case 9: // I see this in data, so might as well "explain" it - fprintf(stream,"Conditional access: "); + fprint_msg_or_err(is_msg,"Conditional access: "); temp_u = (data[0] << 8) | data[1]; - fprintf(stream,"id %04x (%d) ",temp_u,temp_u); + fprint_msg_or_err(is_msg,"id %04x (%d) ",temp_u,temp_u); temp_u = ((data[2] & 0x1F) << 8) | data[3]; - fprintf(stream,"PID %04x (%d) ",temp_u,temp_u); + fprint_msg_or_err(is_msg,"PID %04x (%d) ",temp_u,temp_u); if (data_len > 4) - print_data(stream==stdout,"data",&data[4],data_len-4,data_len-4); + print_data(is_msg,"data",&data[4],data_len-4,data_len-4); else - fprintf(stream,"\n"); + fprint_msg_or_err(is_msg,"\n"); break; case 10: // We'll assume the length is a multiple of 4 - fprintf(stream,"Languages: "); + fprint_msg_or_err(is_msg,"Languages: "); for (ii = 0; ii < this_length/4; ii++) { byte audio_type; - if (ii > 0) fprintf(stream,", "); - putc(*(data+(ii*4)+0),stream); - putc(*(data+(ii*4)+1),stream); - putc(*(data+(ii*4)+2),stream); + if (ii > 0) fprint_msg_or_err(is_msg,", "); + fprint_msg_or_err(is_msg,"%c",*(data+(ii*4)+0)); + fprint_msg_or_err(is_msg,"%c",*(data+(ii*4)+1)); + fprint_msg_or_err(is_msg,"%c",*(data+(ii*4)+2)); audio_type = *(data+(ii*4)+3); switch (audio_type) { - case 0: /*fprintf(stream,"/undefined");*/ break; // clearer to say nowt? - case 1: fprintf(stream,"/clean effects"); break; - case 2: fprintf(stream,"/hearing impaired"); break; - case 3: fprintf(stream,"/visual impaired commentary"); break; - default: fprintf(stream,"/reserved:0x%02x",audio_type); break; + case 0: /*fprint_msg_or_err(is_msg,"/undefined");*/ break; // clearer to say nowt? + case 1: fprint_msg_or_err(is_msg,"/clean effects"); break; + case 2: fprint_msg_or_err(is_msg,"/hearing impaired"); break; + case 3: fprint_msg_or_err(is_msg,"/visual impaired commentary"); break; + default: fprint_msg_or_err(is_msg,"/reserved:0x%02x",audio_type); break; } } - fprintf(stream,"\n"); + fprint_msg_or_err(is_msg,"\n"); break; case 0x56: // teletext for (ii = 0; ii < this_length; ii += 5) @@ -2313,42 +2314,42 @@ extern int print_descriptors(FILE *stream, int jj; int teletext_type, teletext_magazine, teletext_page; if (ii == 0) - fprintf(stream,"Teletext: "); + fprint_msg_or_err(is_msg,"Teletext: "); else { - if (leader1 != NULL) fputs(leader1,stream); - if (leader2 != NULL) fputs(leader2,stream); - fprintf(stream," "); + if (leader1 != NULL) fprint_msg_or_err(is_msg,"%s",leader1); + if (leader2 != NULL) fprint_msg_or_err(is_msg,"%s",leader2); + fprint_msg_or_err(is_msg," "); } - fprintf(stream,"language="); + fprint_msg_or_err(is_msg,"language="); for (jj=ii; jj",data[jj]); + fprint_msg_or_err(is_msg,"<%02x>",data[jj]); } teletext_type = (data[ii+3] & 0xF8) >> 3; teletext_magazine = (data[ii+3] & 0x07); teletext_page = data[ii+4]; - fprintf(stream,", type="); + fprint_msg_or_err(is_msg,", type="); switch (teletext_type) { - case 1: fprintf(stream,"Initial"); break; - case 2: fprintf(stream,"Subtitles"); break; - case 3: fprintf(stream,"Additional info"); break; - case 4: fprintf(stream,"Programme schedule"); break; - case 5: fprintf(stream,"Hearing impaired subtitles"); break; - default: fprintf(stream,"%x (reserved)",teletext_type); break; + case 1: fprint_msg_or_err(is_msg,"Initial"); break; + case 2: fprint_msg_or_err(is_msg,"Subtitles"); break; + case 3: fprint_msg_or_err(is_msg,"Additional info"); break; + case 4: fprint_msg_or_err(is_msg,"Programme schedule"); break; + case 5: fprint_msg_or_err(is_msg,"Hearing impaired subtitles"); break; + default: fprint_msg_or_err(is_msg,"%x (reserved)",teletext_type); break; } - fprintf(stream,", magazine %d, page %x",teletext_magazine,teletext_page); - fprintf(stream,"\n"); + fprint_msg_or_err(is_msg,", magazine %d, page %x",teletext_magazine,teletext_page); + fprint_msg_or_err(is_msg,"\n"); } break; case 0x59: { - fprintf(stream, "subtitling_descriptor:\n"); + fprint_msg_or_err(is_msg, "subtitling_descriptor:\n"); for (ii = 0; ii + 8 <= this_length; ii += 8) { @@ -2360,34 +2361,33 @@ extern int print_descriptors(FILE *stream, lang[1] = data[ii + 1]; lang[2] = data[ii + 2]; lang[3] = 0; - if (leader1 != NULL) fputs(leader1,stream); - if (leader2 != NULL) fputs(leader2,stream); - fprintf(stream, - " language='%s', subtitling_type=%u\n", + if (leader1 != NULL) fprint_msg_or_err(is_msg,"%s",leader1); + if (leader2 != NULL) fprint_msg_or_err(is_msg,"%s",leader2); + fprint_msg_or_err(is_msg," language='%s', subtitling_type=%u\n", lang, subtitling_type); - if (leader1 != NULL) fputs(leader1,stream); - if (leader2 != NULL) fputs(leader2,stream); - fprintf(stream, + if (leader1 != NULL) fprint_msg_or_err(is_msg,"%s",leader1); + if (leader2 != NULL) fprint_msg_or_err(is_msg,"%s",leader2); + fprint_msg_or_err(is_msg, " composition_page_id=%u, ancillary_page_id=%u\n", composition_page_id, ancillary_page_id); } if (ii < this_length) - fprintf(stream, "### %d spare bytes at end of descriptor\n", this_length - ii); + fprint_msg_or_err(is_msg, "### %d spare bytes at end of descriptor\n", this_length - ii); break; } case 0x6A: - print_data(stream==stdout,"DVB AC-3",data,this_length,100); + print_data(is_msg,"DVB AC-3",data,this_length,100); break; case 0x81: - print_data(stream==stdout,"ATSC AC-3",data,this_length,100); + print_data(is_msg,"ATSC AC-3",data,this_length,100); default: // Report the tag number as decimal since that is how H.222 // describes it in table 2-39 { char temp_c[50]; // twice as much as I need... sprintf(temp_c,"Descriptor tag %02x (%3d)",tag,tag); - print_data(stream==stdout,temp_c,data,this_length,100); + print_data(is_msg,temp_c,data,this_length,100); } break; } @@ -2671,7 +2671,7 @@ extern int extract_pmt(int verbose, if (verbose && program_info_length > 0) { printf(" Program info:\n"); - print_descriptors(stdout," ",NULL,&data[12],program_info_length); + print_descriptors(TRUE," ",NULL,&data[12],program_info_length); } // 32 bits at the end of a program association section is reserved for a CRC @@ -2724,7 +2724,7 @@ extern int extract_pmt(int verbose, printf(" PID %04x -> Stream %02x %s\n",pid,stream_type, h222_stream_type_str(stream_type)); if (ES_info_length > 0) - print_descriptors(stdout," ",NULL,&stream_data[5],ES_info_length); + print_descriptors(TRUE," ",NULL,&stream_data[5],ES_info_length); } err = add_stream_to_pmt(*pmt,pid,stream_type,ES_info_length, stream_data+5); @@ -2903,7 +2903,7 @@ extern int extract_stream_list_from_pmt(int verbose, if (verbose && program_info_length > 0) { printf(" Program info:\n"); - print_descriptors(stdout," ",NULL,&data[12],program_info_length); + print_descriptors(TRUE," ",NULL,&data[12],program_info_length); } // 32 bits at the end of a program association section is reserved for a CRC @@ -2949,7 +2949,7 @@ extern int extract_stream_list_from_pmt(int verbose, buf[SARRAYSIZE-1] = '\0'; printf(" Stream %02x %-40s -> PID %04x\n",stream_type,buf,pid); if (ES_info_length > 0) - print_descriptors(stdout," ",NULL,&stream_data[5],ES_info_length); + print_descriptors(TRUE," ",NULL,&stream_data[5],ES_info_length); } // For the moment, we shan't bother to remember the extra info. err = append_to_pidint_list(*stream_list,pid,stream_type); @@ -3442,7 +3442,7 @@ extern int find_pmt(TS_reader_p tsreader, { printf("\n"); printf("Program map\n"); - report_pmt(stdout," ",*pmt); + report_pmt(TRUE," ",*pmt); printf("\n"); } return 0; diff --git a/ts_fns.h b/ts_fns.h index 78be6b7..7280f4b 100644 --- a/ts_fns.h +++ b/ts_fns.h @@ -549,7 +549,7 @@ extern void report_payload(int show_data, * Print out information about program descriptors * (either from the PMT program info, or the PMT/stream ES info) * - * - `stream` is the stream to print on + * - if `is_msg` then print as a message, otherwise as an error * - `leader1` and `leader2` are the text to write at the start of each line * (either or both may be NULL) * - `desc_data` is the data containing the descriptors @@ -557,7 +557,7 @@ extern void report_payload(int show_data, * * Returns 0 if all went well, 1 if something went wrong */ -extern int print_descriptors(FILE *stream, +extern int print_descriptors(int is_msg, char *leader1, char *leader2, byte *desc_data, diff --git a/tsinfo.c b/tsinfo.c index 76fe3b7..ef7f2f1 100644 --- a/tsinfo.c +++ b/tsinfo.c @@ -263,7 +263,7 @@ static int report_streams(TS_reader_p tsreader, else if (!verbose) printf("\nPacket %d is PMT with PID %04x (%d)\n",ii+1,pid,pid); - report_pmt(stdout," ",this_pmt); + report_pmt(TRUE," ",this_pmt); free_pmt(&last_pmt); last_pmt = this_pmt; diff --git a/tsplay.c b/tsplay.c index 7c45b5b..0b50b64 100644 --- a/tsplay.c +++ b/tsplay.c @@ -342,7 +342,7 @@ static int find_PCR_PID(TS_reader_p tsreader, } if (!quiet) - report_pmt(stdout," ",pmt); + report_pmt(TRUE," ",pmt); *pcr_pid = pmt->PCR_pid; free_pmt(&pmt); if (!quiet) diff --git a/tsreport.c b/tsreport.c index 814b6ee..4407878 100644 --- a/tsreport.c +++ b/tsreport.c @@ -745,7 +745,7 @@ static int report_ts(TS_reader_p tsreader, { fprintf(stderr,"### Internal error: stream for PID %0x returned NULL" " in PMT\n",pid); - report_pmt(stderr," ",pmt); + report_pmt(FALSE," ",pmt); free_pidint_list(&prog_list); free_pmt(&pmt); if (pmt_data) free(pmt_data); @@ -904,7 +904,7 @@ static int report_ts(TS_reader_p tsreader, pmt_data = NULL; pmt_data_len = 0; pmt_data_used = 0; #if 0 printf("PMT data read as:\n"); - report_pmt(stdout," ",pmt); + report_pmt(TRUE," ",pmt); printf("\n"); #endif }