From 2f704d0c42ce03f52d75ac547566820521014aac Mon Sep 17 00:00:00 2001 From: tibs Date: Mon, 23 Feb 2009 21:05:51 +0000 Subject: [PATCH] More work on replacing printing functions. --HG-- extra : convert_revision : svn%3Aeff31bef-be4a-0410-a8fe-e47997df2690/trunk%40131 --- adts.c | 37 ++++++----- audio.c | 7 +- avs.c | 189 +++++++++++++++-------------------------------------- avs_fns.h | 9 +-- es.c | 17 +++-- es_fns.h | 4 +- esreport.c | 12 ++-- h262.c | 8 +-- h262_fns.h | 4 +- 9 files changed, 95 insertions(+), 192 deletions(-) diff --git a/adts.c b/adts.c index 9a64304..a6aea67 100644 --- a/adts.c +++ b/adts.c @@ -32,6 +32,7 @@ #include #include "compat.h" +#include "printing_fns.h" #include "misc_fns.h" #include "adts_fns.h" @@ -66,7 +67,7 @@ extern int read_next_adts_frame(int file, offset_t posn = tell_file(file); #if DEBUG - printf("Offset: " OFFSET_T_FORMAT "\n",posn); + fprint_msg("Offset: " OFFSET_T_FORMAT "\n",posn); #endif err = read_bytes(file,JUST_ENOUGH,header); @@ -74,37 +75,37 @@ extern int read_next_adts_frame(int file, return EOF; else if (err) { - fprintf(stderr,"### Error reading header bytes of ADTS frame\n"); - fprintf(stderr," (in frame starting at " OFFSET_T_FORMAT ")\n",posn); + fprint_err("### Error reading header bytes of ADTS frame\n" + " (in frame starting at " OFFSET_T_FORMAT ")\n",posn); free(data); return 1; } #if DEBUG - printf("ADTS frame\n"); + print_msg("ADTS frame\n"); print_data(stdout,"Start",header,JUST_ENOUGH,JUST_ENOUGH); #endif if (header[0] != 0xFF || (header[1] & 0xF0) != 0xF0) { - fprintf(stderr,"### ADTS frame does not start with '1111 1111 1111'" - " syncword - lost synchronisation?\n" - " Found 0x%X%X%X instead of 0xFFF\n", - (unsigned)(header[0] & 0xF0) >> 4, - (header[0] & 0x0F), - (unsigned)(header[1] & 0xF0) >> 4); - fprintf(stderr," (in frame starting at " OFFSET_T_FORMAT ")\n",posn); + fprint_err("### ADTS frame does not start with '1111 1111 1111'" + " syncword - lost synchronisation?\n" + " Found 0x%X%X%X instead of 0xFFF\n", + (unsigned)(header[0] & 0xF0) >> 4, + (header[0] & 0x0F), + (unsigned)(header[1] & 0xF0) >> 4); + fprint_err(" (in frame starting at " OFFSET_T_FORMAT ")\n",posn); return 1; } id = (header[1] & 0x08) >> 3; #if DEBUG - printf(" ID %d (%s)\n",id,(id==1?"MPEG-2 AAC":"MPEG-4")); + fprint_msg(" ID %d (%s)\n",id,(id==1?"MPEG-2 AAC":"MPEG-4")); #endif layer = (header[1] & 0x06) >> 1; if (layer != 0) - printf(" layer is %d, not 0 (in frame at " OFFSET_T_FORMAT ")\n", - layer,posn); + fprint_msg(" layer is %d, not 0 (in frame at " OFFSET_T_FORMAT ")\n", + layer,posn); // Experience appears to show that emphasis doesn't exist in MPEG-2 AVC. // But it does exist in (ID=1) MPEG-4 streams. @@ -124,13 +125,13 @@ extern int read_next_adts_frame(int file, frame_length = (header[4] << 5) | ((unsigned)(header[5] & 0xF8) >> 3); } #if DEBUG - printf(" length %d\n",frame_length); + fprint_msg(" length %d\n",frame_length); #endif data = malloc(frame_length); if (data == NULL) { - fprintf(stderr,"### Unable to extend data buffer for ADTS frame\n"); + print_err("### Unable to extend data buffer for ADTS frame\n"); free(data); return 1; } @@ -142,9 +143,9 @@ extern int read_next_adts_frame(int file, if (err) { if (err == EOF) - fprintf(stderr,"### Unexpected EOF reading rest of ADTS frame\n"); + print_err("### Unexpected EOF reading rest of ADTS frame\n"); else - fprintf(stderr,"### Error reading rest of ADTS frame\n"); + print_err("### Error reading rest of ADTS frame\n"); free(data); return 1; } diff --git a/audio.c b/audio.c index 6c6e54b..4f3e50f 100644 --- a/audio.c +++ b/audio.c @@ -32,6 +32,7 @@ #include #include "compat.h" +#include "printing_fns.h" #include "audio_fns.h" #include "adts_fns.h" #include "l2audio_fns.h" @@ -47,7 +48,7 @@ extern int build_audio_frame(audio_frame_p *frame) audio_frame_p new = malloc(SIZEOF_AUDIO_FRAME); if (new == NULL) { - fprintf(stderr,"### Unable to allocate audio frame datastructure\n"); + fprint_err("### Unable to allocate audio frame datastructure\n"); return 1; } @@ -110,8 +111,8 @@ extern int read_next_audio_frame(int file, case AUDIO_AC3: return read_next_ac3_frame(file, frame); default: - fprintf(stderr,"### Unrecognised audio type %d - cannot get next audio frame\n", - audio_type); + fprint_err("### Unrecognised audio type %d - cannot get next audio frame\n", + audio_type); return 1; } } diff --git a/avs.c b/avs.c index 7d14476..fdfe85b 100644 --- a/avs.c +++ b/avs.c @@ -38,6 +38,7 @@ #include #include "compat.h" +#include "printing_fns.h" #include "avs_fns.h" #include "es_fns.h" #include "ts_fns.h" @@ -69,21 +70,6 @@ extern const char *avs_start_code_str(byte start_code) } } -/* - * Print out information derived from the start code, to the given stream. - */ -extern void print_avs_start_code_str(FILE *stream, - byte start_code) -{ - if (stream != NULL) - { - if (start_code < 0xB0) - fprintf(stream,"Slice %02x\n",start_code & 0xAF); - else - fprintf(stream,"%s",avs_start_code_str(start_code)); - } -} - // ------------------------------------------------------------ // AVS item *data* stuff // ------------------------------------------------------------ @@ -102,7 +88,7 @@ extern int build_avs_context(ES_p es, avs_context_p new = malloc(SIZEOF_AVS_CONTEXT); if (new == NULL) { - fprintf(stderr,"### Unable to allocate AVS context datastructure\n"); + print_err("### Unable to allocate AVS context datastructure\n"); return 1; } @@ -214,15 +200,15 @@ extern int avs_picture_coding_type(ES_unit_p unit) return picture_coding_type; else { - fprintf(stderr,"AVS Picture coding type %d (in %02x)\n", - picture_coding_type,unit->data[3]); + fprint_err("AVS Picture coding type %d (in %02x)\n", + picture_coding_type,unit->data[3]); return 0; } } else { - fprintf(stderr,"AVS 'frame' with start code %02x does not have picture coding type\n", - unit->data[0]); + fprint_err("AVS 'frame' with start code %02x does not have picture coding type\n", + unit->data[0]); return 0; } } @@ -242,14 +228,14 @@ static int build_avs_frame(avs_context_p context, avs_frame_p new = malloc(SIZEOF_AVS_FRAME); if (new == NULL) { - fprintf(stderr,"### Unable to allocate AVS frame datastructure\n"); + print_err("### Unable to allocate AVS frame datastructure\n"); return 1; } err = build_ES_unit_list(&(new->list)); if (err) { - fprintf(stderr,"### Unable to allocate internal list for AVS frame\n"); + print_err("### Unable to allocate internal list for AVS frame\n"); free(new); return 1; } @@ -276,8 +262,8 @@ static int build_avs_frame(avs_context_p context, new->aspect_ratio = (data[10] & 0x3C) >> 2; new->frame_rate_code = ((data[10] & 0x03) << 2) | ((data[11] & 0xC0) >> 4); #if DEBUG - printf("aspect_ratio=%d, frame_rate_code=%d (%.2f)\n",new->aspect_ratio, - new->frame_rate_code,avs_frame_rate(new->frame_rate_code)); + fprint_msg("aspect_ratio=%d, frame_rate_code=%d (%.2f)\n",new->aspect_ratio, + new->frame_rate_code,avs_frame_rate(new->frame_rate_code)); #endif } else if (is_avs_seq_end_item(unit)) @@ -288,9 +274,8 @@ static int build_avs_frame(avs_context_p context, } else { - fprintf(stderr, - "!!! Building AVS frame that starts with a %s (%02x)\n", - avs_start_code_str(unit->start_code),unit->start_code); + fprint_err("!!! Building AVS frame that starts with a %s (%02x)\n", + avs_start_code_str(unit->start_code),unit->start_code); new->is_frame = FALSE; new->is_sequence_header = FALSE; new->picture_coding_type = 0xFF; // Meaningless value, just in case @@ -299,8 +284,8 @@ static int build_avs_frame(avs_context_p context, err = append_to_avs_frame(new,unit); if (err) { - fprintf(stderr,"### Error appending first ES unit to AVS %s\n", - avs_start_code_str(unit->start_code)); + fprint_err("### Error appending first ES unit to AVS %s\n", + avs_start_code_str(unit->start_code)); free_avs_frame(&new); return 1; } @@ -331,94 +316,23 @@ extern void free_avs_frame(avs_frame_p *frame) return; } -#if 0 -/* - * Remember a frame for future reversing, if it's an I frame or a - * sequence header - * - * Returns 0 if it succeeds, 1 if some error occurs. - */ -static int maybe_remember_this_frame(avs_context_p avs, - int verbose, - avs_frame_p this_frame) -{ - int err; - ES_offset start_posn = {0,0}; - uint32_t num_bytes = 0; - if (this_frame->is_frame) - { - if (this_frame->picture_coding_type == AVS_I_PICTURE_CODING) - { - // It's an I frame - we want to remember it in our reverse list - (avs->count_since_seq_hdr) ++; - - err = get_ES_unit_list_bounds(this_frame->list,&start_posn,&num_bytes); - if (err) - { - fprintf(stderr, - "### Error working out position/size of AVS frame\n"); - return 1; - } - - err = remember_reverse_avs_data(avs->reverse_data,avs->frame_index, - start_posn,num_bytes, - avs->count_since_seq_hdr,0); - if (err) - { - fprintf(stderr, - "### Error remembering reversing data for AVS item\n"); - return 1; - } - if (verbose) - printf("REMEMBER I frame %5d at " OFFSET_T_FORMAT_08 - "/%04d for %5d\n",avs->frame_index, - start_posn.infile,start_posn.inpacket,num_bytes); - } - } - else if (this_frame->is_sequence_header) - { - // It's a sequence header - remember it for the next frame - avs->count_since_seq_hdr = 0; - err = get_ES_unit_list_bounds(this_frame->list,&start_posn,&num_bytes); - if (err) - { - fprintf(stderr,"### Error working out position/size of AVS" - " sequence header for reversing data\n"); - return 1; - } - err = remember_reverse_avs_data(avs->reverse_data,0, - start_posn,num_bytes,0); - if (err) - { - fprintf(stderr,"### Error remembering reversing data for AVS item\n"); - return 1; - } - if (verbose) - printf("REMEMBER Sequence header at " OFFSET_T_FORMAT_08 - "/%04d for %5d\n", - start_posn.infile,start_posn.inpacket,num_bytes); - } - return 0; -} -#endif - #if DEBUG_GET_NEXT_PICTURE /* * Print a representation of an item for debugging */ static void _show_item(ES_unit_p unit) { - printf("__ "); + print_msg("__ "); if (unit == NULL) { - printf("\n"); + print_msg("\n"); return; } if (is_avs_frame_item(unit)) - printf("%s frame",AVS_PICTURE_CODING_STR(avs_picture_coding_type(unit))); + fprint_msg("%s frame",AVS_PICTURE_CODING_STR(avs_picture_coding_type(unit))); else - printf("%s",avs_start_code_str(unit->start_code)); - printf(" at " OFFSET_T_FORMAT "/%d for %d\n", + fprint_msg("%s",avs_start_code_str(unit->start_code)); + fprint_msg(" at " OFFSET_T_FORMAT "/%d for %d\n", unit->start_posn.infile,unit->start_posn.inpacket,unit->data_len); } #endif @@ -458,7 +372,7 @@ static int get_next_avs_single_frame(avs_context_p context, int num_slices = 0; int had_slice = FALSE; int last_slice_start_code = 0; - if (verbose && context->last_item) printf("__ reuse last item\n"); + if (verbose && context->last_item) print_msg("__ reuse last item\n"); #endif context->last_item = NULL; @@ -496,7 +410,7 @@ static int get_next_avs_single_frame(avs_context_p context, #if DEBUG_GET_NEXT_PICTURE if (verbose) { - printf("__ --------------------------------- \n"); + print_msg("__ --------------------------------- \n"); _show_item(item); } #endif @@ -511,7 +425,7 @@ static int get_next_avs_single_frame(avs_context_p context, // A sequence end is a single item, so we're done #if DEBUG_GET_NEXT_PICTURE if (verbose) - printf("__ --------------------------------- \n"); + print_msg("__ --------------------------------- \n"); #endif return 0; } @@ -566,7 +480,7 @@ static int get_next_avs_single_frame(avs_context_p context, err = append_to_avs_frame(*frame,item); if (err) { - fprintf(stderr,"### Error adding item to AVS sequence header\n"); + print_err("### Error adding item to AVS sequence header\n"); free_avs_frame(frame); return 1; } @@ -585,15 +499,15 @@ static int get_next_avs_single_frame(avs_context_p context, if (num_slices > 1) { ES_unit_p unit = &(*frame)->list->array[(*frame)->list->length-1]; - printf("__ ...\n"); - printf("__ slice %2x",last_slice_start_code); - printf(" at " OFFSET_T_FORMAT "/%d for %d\n", - unit->start_posn.infile,unit->start_posn.inpacket, - unit->data_len); + print_msg("__ ...\n"); + fprint_msg("__ slice %2x",last_slice_start_code); + fprint_msg(" at " OFFSET_T_FORMAT "/%d for %d\n", + unit->start_posn.infile,unit->start_posn.inpacket, + unit->data_len); } - printf("__ (%2d slices)\n",num_slices); + fprint_msg("__ (%2d slices)\n",num_slices); } - printf("__ --------------------------------- \n"); + print_msg("__ --------------------------------- \n"); if (in_frame || in_sequence_header) _show_item(item); } @@ -682,7 +596,7 @@ extern int write_avs_frame_as_TS(TS_writer_p tswriter, DEFAULT_VIDEO_STREAM_ID); if (err) { - fprintf(stderr,"### Error writing out frame list to TS\n"); + print_err("### Error writing out frame list to TS\n"); return err; } } @@ -742,7 +656,7 @@ extern int write_avs_frame_as_TS_with_pts_dts(avs_frame_p frame, video_pid,DEFAULT_VIDEO_STREAM_ID); if (err) { - fprintf(stderr,"### Error writing out frame list to TS\n"); + print_err("### Error writing out frame list to TS\n"); return err; } } @@ -793,7 +707,7 @@ extern int write_avs_frame_as_TS_with_PCR(avs_frame_p frame, video_pid,DEFAULT_VIDEO_STREAM_ID); if (err) { - fprintf(stderr,"### Error writing out frame list to TS\n"); + print_err("### Error writing out frame list to TS\n"); return err; } } @@ -826,7 +740,7 @@ extern int write_avs_frame_as_ES(FILE *output, err = write_ES_unit(output,unit); if (err) { - fprintf(stderr,"### Error writing out frame list to ES\n"); + print_err("### Error writing out frame list to ES\n"); return err; } } @@ -840,36 +754,35 @@ extern int write_avs_frame_as_ES(FILE *output, * - `frame` is the frame to report on * - if `report_data`, then the component ES units will be printed out as well */ -extern void report_avs_frame(FILE *stream, - avs_frame_p frame, +extern void report_avs_frame(avs_frame_p frame, int report_data) { if (frame->is_frame) { - printf("%s #%02d", - AVS_PICTURE_CODING_STR(frame->picture_coding_type), - frame->picture_distance); - printf("\n"); + fprint_msg("%s #%02d", + AVS_PICTURE_CODING_STR(frame->picture_coding_type), + frame->picture_distance); + print_msg("\n"); } else if (frame->is_sequence_header) { - printf("Sequence header: "); - printf(" frame rate %d (%.2f), aspect ratio %d (%s)", - frame->frame_rate_code, - avs_frame_rate(frame->frame_rate_code), - frame->aspect_ratio, - (frame->aspect_ratio==1?"SAR: 1.0": - frame->aspect_ratio==2?"4/3": - frame->aspect_ratio==3?"16/9": - frame->aspect_ratio==4?"2.21/1":"???")); - printf("\n"); + print_msg("Sequence header: "); + fprint_msg(" frame rate %d (%.2f), aspect ratio %d (%s)", + frame->frame_rate_code, + avs_frame_rate(frame->frame_rate_code), + frame->aspect_ratio, + (frame->aspect_ratio==1?"SAR: 1.0": + frame->aspect_ratio==2?"4/3": + frame->aspect_ratio==3?"16/9": + frame->aspect_ratio==4?"2.21/1":"???")); + print_msg("\n"); } else { - printf("Sequence end\n"); + print_msg("Sequence end\n"); } if (report_data) - report_ES_unit_list(stream,"ES units",frame->list); + report_ES_unit_list("ES units",frame->list); } // Local Variables: diff --git a/avs_fns.h b/avs_fns.h index c992339..c237cfe 100644 --- a/avs_fns.h +++ b/avs_fns.h @@ -35,11 +35,6 @@ * Return a string representing the start code */ extern const char *avs_start_code_str(byte start_code); -/* - * Print out information derived from the start code, to the given stream. - */ -extern void print_avs_start_code_str(FILE *stream, - byte start_code); /* * Determine the picture coding type of an AVS ES unit * @@ -185,12 +180,10 @@ extern int write_avs_frame_as_ES(FILE *output, /* * Report on an AVS frame's contents. * - * - `stream` is where to write the information * - `frame` is the frame to report on * - if `report_data`, then the component ES units will be printed out as well */ -extern void report_avs_frame(FILE *stream, - avs_frame_p frame, +extern void report_avs_frame(avs_frame_p frame, int report_data); #endif // _avs_fns diff --git a/es.c b/es.c index be8df51..cfe5040 100644 --- a/es.c +++ b/es.c @@ -37,6 +37,7 @@ #endif // _WIN32 #include "compat.h" +#include "printing_fns.h" #include "misc_fns.h" #include "pes_fns.h" #include "tswrite_fns.h" @@ -1229,26 +1230,24 @@ extern void free_ES_unit_list(ES_unit_list_p *list) /* * Report on an ES unit list's contents. * - * - `stream` is where to write the information * - `name` is the name of the list (used in the header) * - `list` is the list to report on */ -extern void report_ES_unit_list(FILE *stream, - char *name, +extern void report_ES_unit_list(char *name, ES_unit_list_p list) { - fprintf(stream,"ES unit list '%s': ",name); + fprint_msg("ES unit list '%s': ",name); if (list->array == NULL) - fprintf(stream,"\n"); + print_msg("\n"); else { int ii; - fprintf(stream,"%d item%s (size %d)\n",list->length, - (list->length==1?"":"s"),list->size); + fprint_msg("%d item%s (size %d)\n",list->length, + (list->length==1?"":"s"),list->size); for (ii=0; iilength; ii++) { - fprintf(stream," "); - report_ES_unit(stream,&(list->array[ii])); + print_msg(" "); + report_ES_unit(stdout,&(list->array[ii])); } } } diff --git a/es_fns.h b/es_fns.h index 0892b64..899ee73 100644 --- a/es_fns.h +++ b/es_fns.h @@ -335,12 +335,10 @@ extern void free_ES_unit_list(ES_unit_list_p *list); /* * Report on an ES unit list's contents. * - * - `stream` is where to write the information * - `name` is the name of the list (used in the header) * - `list` is the list to report on */ -extern void report_ES_unit_list(FILE *stream, - char *name, +extern void report_ES_unit_list(char *name, ES_unit_list_p list); /* diff --git a/esreport.c b/esreport.c index af75200..23ef5c1 100644 --- a/esreport.c +++ b/esreport.c @@ -116,9 +116,9 @@ static void report_avs_frames(ES_p es, count++; if (!quiet) - report_avs_frame(stdout,frame,FALSE); + report_avs_frame(frame,FALSE); else if (verbose) - report_avs_frame(stdout,frame,TRUE); + report_avs_frame(frame,TRUE); if (frame->is_frame) { @@ -306,7 +306,7 @@ static void find_h262_fields(ES_p es, { if (picture->picture_structure < 3) { - report_h262_picture(stdout,picture,verbose); + report_h262_picture(picture,verbose); num_fields ++; } else @@ -400,9 +400,9 @@ static void report_h262_frames(ES_p es, count++; if (!quiet) - report_h262_picture(stdout,picture,FALSE); + report_h262_picture(picture,FALSE); else if (verbose) - report_h262_picture(stdout,picture,TRUE); + report_h262_picture(picture,TRUE); if (picture->is_picture) { @@ -554,7 +554,7 @@ static void report_h262_afds(ES_p es, double seconds = total_seconds - 60*minutes; printf("%dm %4.1fs (frame %d @ %.2fs): ",minutes,seconds, frames,total_seconds); - report_h262_picture(stdout,picture,FALSE); + report_h262_picture(picture,FALSE); afd = picture->afd; } diff --git a/h262.c b/h262.c index fd1cc3a..5f4dcca 100644 --- a/h262.c +++ b/h262.c @@ -32,6 +32,7 @@ #include #include "compat.h" +#include "printing_fns.h" #include "h262_fns.h" #include "es_fns.h" #include "ts_fns.h" @@ -1147,8 +1148,7 @@ extern int write_h262_picture_as_ES(FILE *output, * - `picture` is the picture to report on * - if `report_data`, then the component ES units will be printed out as well */ -extern void report_h262_picture(FILE *stream, - h262_picture_p picture, +extern void report_h262_picture(h262_picture_p picture, int report_data) { if (picture->is_picture) @@ -1162,7 +1162,7 @@ extern void report_h262_picture(FILE *stream, printf(" (merged)"); printf(" %s",H262_ASPECT_RATIO_INFO_STR(picture->aspect_ratio_info)); - + if (picture->is_real_afd) printf(" AFD "); else @@ -1193,7 +1193,7 @@ extern void report_h262_picture(FILE *stream, printf("Sequence end\n"); } if (report_data) - report_ES_unit_list(stream,"ES units",picture->list); + report_ES_unit_list("ES units",picture->list); } // Local Variables: diff --git a/h262_fns.h b/h262_fns.h index db027ab..8a8d123 100644 --- a/h262_fns.h +++ b/h262_fns.h @@ -224,12 +224,10 @@ extern int write_h262_picture_as_ES(FILE *output, /* * Report on an H.262 picture's contents. * - * - `stream` is where to write the information * - `picture` is the picture to report on * - if `report_data`, then the component ES units will be printed out as well */ -extern void report_h262_picture(FILE *stream, - h262_picture_p picture, +extern void report_h262_picture(h262_picture_p picture, int report_data); #endif // _h262_fns