From ad059b4d61adb7a46ebc22d90f56ed8f7cb3d578 Mon Sep 17 00:00:00 2001 From: tibs Date: Sun, 3 May 2009 16:18:16 +0000 Subject: [PATCH] Next step in message tidying - es.c and friends. --HG-- extra : convert_revision : svn%3Aeff31bef-be4a-0410-a8fe-e47997df2690/trunk%40137 --- es.c | 208 ++++++++++++++++++++++++++--------------------------- es2ts.c | 2 +- es_fns.h | 4 +- esreport.c | 2 +- h262.c | 200 +++++++++++++++++++++++++-------------------------- h262_fns.h | 5 +- pes.c | 2 +- 7 files changed, 205 insertions(+), 218 deletions(-) diff --git a/es.c b/es.c index cfe5040..fb490fe 100644 --- a/es.c +++ b/es.c @@ -82,8 +82,7 @@ extern int open_elementary_stream(char *filename, err = build_elementary_stream_file(input,es); if (err) { - fprintf(stderr,"### Error building elementary stream for file %s\n", - filename); + fprint_err("### Error building elementary stream for file %s\n", filename); return 1; } return 0; @@ -111,8 +110,8 @@ static int setup_readahead(ES_p es) { if (es->read_ahead_len < 3) { - fprintf(stderr,"### File only contains %d byte%s\n", - es->read_ahead_len,(es->read_ahead_len==1?"":"s")); + fprint_err("### File only contains %d byte%s\n", + es->read_ahead_len,(es->read_ahead_len==1?"":"s")); return 1; } } @@ -120,15 +119,15 @@ static int setup_readahead(ES_p es) { if (es->reader->packet->es_data_len < 3) { - fprintf(stderr,"### File PES packet only contains %d byte%s\n", - es->reader->packet->es_data_len, - (es->reader->packet->es_data_len==1?"":"s")); + fprint_err("### File PES packet only contains %d byte%s\n", + es->reader->packet->es_data_len, + (es->reader->packet->es_data_len==1?"":"s")); return 1; } } if (DEBUG) - printf("File starts %02x %02x %02x\n",es->data[0],es->data[1],es->data[2]); + fprint_msg("File starts %02x %02x %02x\n",es->data[0],es->data[1],es->data[2]); // Despite (maybe) reporting the above, we haven't actually read anything // yet @@ -156,7 +155,7 @@ extern int build_elementary_stream_file(int input, ES_p new = malloc(SIZEOF_ES); if (new == NULL) { - fprintf(stderr,"### Unable to allocate elementary stream datastructure\n"); + print_err("### Unable to allocate elementary stream datastructure\n"); return 1; } @@ -189,7 +188,7 @@ extern int build_elementary_stream_PES(PES_reader_p reader, ES_p new = malloc(SIZEOF_ES); if (new == NULL) { - fprintf(stderr,"### Unable to allocate elementary stream datastructure\n"); + print_err("### Unable to allocate elementary stream datastructure\n"); return 1; } @@ -282,7 +281,7 @@ extern int setup_ES_unit(ES_unit_p unit) unit->data = malloc(ES_UNIT_DATA_START_SIZE); if (unit->data == NULL) { - fprintf(stderr,"### Unable to allocate ES unit data buffer\n"); + print_err("### Unable to allocate ES unit data buffer\n"); return 1; } unit->data_len = 0; @@ -321,7 +320,7 @@ extern int build_ES_unit(ES_unit_p *unit) ES_unit_p new = malloc(SIZEOF_ES_UNIT); if (new == NULL) { - fprintf(stderr,"### Unable to allocate ES unit datastructure\n"); + print_err("### Unable to allocate ES unit datastructure\n"); return 1; } err = setup_ES_unit(new); @@ -349,13 +348,13 @@ extern int build_ES_unit_from_data(ES_unit_p *unit, ES_unit_p new = malloc(SIZEOF_ES_UNIT); if (new == NULL) { - fprintf(stderr,"### Unable to allocate ES unit datastructure\n"); + print_err("### Unable to allocate ES unit datastructure\n"); return 1; } new->data = malloc(data_len); if (new->data == NULL) { - fprintf(stderr,"### Unable to allocate ES unit data buffer\n"); + print_err("### Unable to allocate ES unit data buffer\n"); return 1; } (void) memcpy(new->data, data, data_len); @@ -386,16 +385,17 @@ extern void free_ES_unit(ES_unit_p *unit) } /* - * Print out some information this ES unit, on the given stream + * Print out some information this ES unit, on normal or error output */ -extern void report_ES_unit(FILE *stream, +extern void report_ES_unit(int is_msg, ES_unit_p unit) { byte s = unit->start_code; - fprintf(stream,OFFSET_T_FORMAT_08 "/%4d: ES unit (%02x '%d%d%d%d %d%d%d%d')", - unit->start_posn.infile,unit->start_posn.inpacket,s, - (s&0x80)>>7,(s&0x40)>>6,(s&0x20)>>5,(s&0x10)>>4, - (s&0x08)>>3,(s&0x04)>>2,(s&0x02)>>1,(s&0x01)); + fprint_msg_or_err(is_msg, + OFFSET_T_FORMAT_08 "/%4d: ES unit (%02x '%d%d%d%d %d%d%d%d')", + unit->start_posn.infile,unit->start_posn.inpacket,s, + (s&0x80)>>7,(s&0x40)>>6,(s&0x20)>>5,(s&0x10)>>4, + (s&0x08)>>3,(s&0x04)>>2,(s&0x02)>>1,(s&0x01)); // Show the data bytes - but we don't need to show the first 4, // since we know they're 00 00 01 @@ -404,13 +404,13 @@ extern void report_ES_unit(FILE *stream, int ii; int data_len = unit->data_len - 4; int show_len = (data_len>10?10:data_len); - fprintf(stream," %6d:",data_len); + fprint_msg_or_err(is_msg," %6d:",data_len); for (ii = 0; ii < show_len; ii++) - fprintf(stream," %02x",unit->data[4+ii]); + fprint_msg_or_err(is_msg," %02x",unit->data[4+ii]); if (show_len < data_len) - fprintf(stream,"..."); + fprint_msg_or_err(is_msg,"..."); } - fprintf(stream,"\n"); + fprint_msg_or_err(is_msg,"\n"); } // ------------------------------------------------------------ @@ -477,7 +477,7 @@ static inline int get_more_data(ES_p es) return EOF; else if (len == -1) { - fprintf(stderr,"### Error reading next bytes: %s\n",strerror(errno)); + fprint_err("### Error reading next bytes: %s\n",strerror(errno)); return 1; } es->read_ahead_posn += es->read_ahead_len; // length of the *last* buffer @@ -627,7 +627,7 @@ static int find_ES_unit_end(ES_p es, unit->data = realloc(unit->data,newsize); if (unit->data == NULL) { - fprintf(stderr,"### Unable to extend ES unit data array\n"); + print_err("### Unable to extend ES unit data array\n"); return 1; } unit->data_size = newsize; @@ -754,9 +754,9 @@ extern int write_ES_unit(FILE *output, size_t written = fwrite(unit->data,1,unit->data_len,output); if (written != unit->data_len) { - fprintf(stderr,"### Error writing out ES unit data: %s\n" - " Wrote %ld bytes instead of %d\n", - strerror(errno),(long int)written,unit->data_len); + fprint_err("### Error writing out ES unit data: %s\n" + " Wrote %ld bytes instead of %d\n", + strerror(errno),(long int)written,unit->data_len); return 1; } else @@ -782,8 +782,8 @@ static int seek_in_PES(ES_p es, if (es->reader == NULL) { - fprintf(stderr,"### Attempt to seek in PES for an ES reader that" - " is not attached to a PES reader\n"); + print_err("### Attempt to seek in PES for an ES reader that" + " is not attached to a PES reader\n"); return 1; } @@ -795,25 +795,25 @@ static int seek_in_PES(ES_p es, err = set_PES_reader_position(es->reader,where.infile); if (err) { - fprintf(stderr,"### Error seeking for PES packet at " OFFSET_T_FORMAT - "\n",where.infile); + fprint_err("### Error seeking for PES packet at " OFFSET_T_FORMAT + "\n",where.infile); return 1; } // Read the PES packet containing ES (ignoring packets we don't care about) err = get_next_pes_packet(es); if (err) { - fprintf(stderr,"### Error reading PES packet at " OFFSET_T_FORMAT "/%d\n", - where.infile,where.inpacket); + fprint_err("### Error reading PES packet at " OFFSET_T_FORMAT "/%d\n", + where.infile,where.inpacket); return 1; } // Now sort out the byte offset if (where.inpacket > es->reader->packet->es_data_len) { - fprintf(stderr,"### Error seeking PES packet at " OFFSET_T_FORMAT "/%d: " - " packet ES data is only %d bytes long\n",where.infile, - where.inpacket,es->reader->packet->es_data_len); + fprint_err("### Error seeking PES packet at " OFFSET_T_FORMAT "/%d: " + " packet ES data is only %d bytes long\n",where.infile, + where.inpacket,es->reader->packet->es_data_len); return 1; } es->posn_of_next_byte = where; @@ -878,9 +878,8 @@ extern int seek_ES(ES_p es, err = seek_in_PES(es,where); if (err) { - fprintf(stderr, - "### Error seeking within ES over PES (offset " OFFSET_T_FORMAT - "/%d)\n",where.infile,where.inpacket); + fprint_err("### Error seeking within ES over PES (offset " OFFSET_T_FORMAT + "/%d)\n",where.infile,where.inpacket); return 1; } } @@ -977,7 +976,7 @@ extern int read_ES_data(ES_p es, *data = realloc(*data,num_bytes); if (*data == NULL) { - fprintf(stderr,"### Unable to reallocate data space\n"); + print_err("### Unable to reallocate data space\n"); return 1; } if (data_len != NULL) @@ -992,7 +991,7 @@ extern int read_ES_data(ES_p es, { if (err == EOF) { - fprintf(stderr,"### Error (EOF) reading %d bytes\n",num_bytes); + fprint_err("### Error (EOF) reading %d bytes\n",num_bytes); return 1; } else @@ -1005,7 +1004,7 @@ extern int read_ES_data(ES_p es, err = read_bytes_from_PES(es,*data,num_bytes); if (err) { - fprintf(stderr,"### Error reading %d bytes from PES\n",num_bytes); + fprint_err("### Error reading %d bytes from PES\n",num_bytes); return 1; } } @@ -1044,8 +1043,8 @@ extern int get_end_of_underlying_PES_packet(ES_p es, if (es->reading_ES) { - fprintf(stderr,"### Cannot retrieve end of PES packet - the ES data" - " is direct ES, not ES read from PES\n"); + fprint_err("### Cannot retrieve end of PES packet - the ES data" + " is direct ES, not ES read from PES\n"); return 1; } if (es->reader->packet == NULL) @@ -1092,7 +1091,7 @@ extern int get_end_of_underlying_PES_packet(ES_p es, *data = malloc(*data_len); if (*data == NULL) { - fprintf(stderr,"### Cannot allocate space for rest of PES packet\n"); + print_err("### Cannot allocate space for rest of PES packet\n"); return 1; } (*data)[0] = es->prev2_byte; // Hmm - should be 0x00 @@ -1118,7 +1117,7 @@ extern int build_ES_unit_list(ES_unit_list_p *list) ES_unit_list_p new = malloc(SIZEOF_ES_UNIT_LIST); if (new == NULL) { - fprintf(stderr,"### Unable to allocate ES unit list datastructure\n"); + print_err("### Unable to allocate ES unit list datastructure\n"); return 1; } @@ -1128,8 +1127,7 @@ extern int build_ES_unit_list(ES_unit_list_p *list) if (new->array == NULL) { free(new); - fprintf(stderr, - "### Unable to allocate array in ES unit list datastructure\n"); + print_err("### Unable to allocate array in ES unit list datastructure\n"); return 1; } *list = new; @@ -1154,7 +1152,7 @@ extern int append_to_ES_unit_list(ES_unit_list_p list, list->array = realloc(list->array,newsize*SIZEOF_ES_UNIT); if (list->array == NULL) { - fprintf(stderr,"### Unable to extend ES unit list array\n"); + print_err("### Unable to extend ES unit list array\n"); return 1; } list->size = newsize; @@ -1166,7 +1164,7 @@ extern int append_to_ES_unit_list(ES_unit_list_p list, ptr->data = malloc(unit->data_len); if (ptr->data == NULL) { - fprintf(stderr,"### Unable to copy ES unit data array\n"); + print_err("### Unable to copy ES unit data array\n"); return 1; } memcpy(ptr->data,unit->data,unit->data_len); @@ -1247,7 +1245,7 @@ extern void report_ES_unit_list(char *name, for (ii=0; iilength; ii++) { print_msg(" "); - report_ES_unit(stdout,&(list->array[ii])); + report_ES_unit(TRUE,&(list->array[ii])); } } } @@ -1269,8 +1267,7 @@ extern int get_ES_unit_list_bounds(ES_unit_list_p list, int ii; if (list->array == NULL || list->length == 0) { - fprintf(stderr, - "### Cannot determine bounds of an ES unit list with no content\n"); + print_err("### Cannot determine bounds of an ES unit list with no content\n"); return 1; } @@ -1362,7 +1359,7 @@ static int try_to_guess_video_type(ES_unit_p unit, byte nal_unit_type = 0; if (show_reasoning) - printf("Looking at ES unit with start code %02X\n",unit->start_code); + fprint_msg("Looking at ES unit with start code %02X\n",unit->start_code); // The following are *not allowed* // @@ -1372,19 +1369,17 @@ static int try_to_guess_video_type(ES_unit_p unit, if (unit->start_code == 0xBA) // PS pack header { - fprintf(stderr, - "### ES unit start code is 0xBA, which looks like a PS pack" - " header\n i.e., data may be PS\n"); + print_err("### ES unit start code is 0xBA, which looks like a PS pack" + " header\n i.e., data may be PS\n"); return 1; } if (unit->start_code >= 0xB9) // system start code - probably PES { - fprintf(stderr, - "### ES unit start code %02X is more than 0xB9, which is probably" - " a PES system start code\n i.e., data may be PES, " - "and is thus probably PS or TS\n", - unit->start_code); + fprint_err("### ES unit start code %02X is more than 0xB9, which is probably" + " a PES system start code\n i.e., data may be PES, " + "and is thus probably PS or TS\n", + unit->start_code); return 1; } @@ -1392,7 +1387,7 @@ static int try_to_guess_video_type(ES_unit_p unit, { if (*maybe_h264) { - if (show_reasoning) printf(" %02X has top bit set, so not H.264,\n",unit->start_code); + if (show_reasoning) fprint_msg(" %02X has top bit set, so not H.264,\n",unit->start_code); *maybe_h264 = FALSE; } @@ -1402,43 +1397,43 @@ static int try_to_guess_video_type(ES_unit_p unit, { *maybe_h262 = FALSE; if (show_reasoning) - printf(" Start code %02X is reserved in H.262, so not H.262\n", - unit->start_code); + fprint_msg(" Start code %02X is reserved in H.262, so not H.262\n", + unit->start_code); } else if (unit->start_code == 0xB4 || unit->start_code == 0xB8) { *maybe_avs = FALSE; if (show_reasoning) - printf(" Start code %02X is reserved in AVS, so not AVS\n", - unit->start_code); + fprint_msg(" Start code %02X is reserved in AVS, so not AVS\n", + unit->start_code); } } else if (*maybe_h264) { if (show_reasoning) - printf(" Top bit not set, so might be H.264\n"); + print_msg(" Top bit not set, so might be H.264\n"); // If we don't have that top bit set, then we need to work a bit harder nal_ref_idc = (unit->start_code & 0x60) >> 5; nal_unit_type = (unit->start_code & 0x1F); if (show_reasoning) - printf(" Interpreting it as nal_ref_idc %d, nal_unit_type %d\n", - nal_ref_idc,nal_unit_type); + fprint_msg(" Interpreting it as nal_ref_idc %d, nal_unit_type %d\n", + nal_ref_idc,nal_unit_type); if (nal_unit_type > 12 && nal_unit_type < 24) { if (show_reasoning) - printf(" H.264 reserves nal_unit_type %02X," - " so not H.264\n",nal_unit_type); + fprint_msg(" H.264 reserves nal_unit_type %02X," + " so not H.264\n",nal_unit_type); *maybe_h264 = FALSE; } else if (nal_unit_type > 23) { if (show_reasoning) - printf(" H.264 does not specify nal_unit_type %02X," - " so not H.264\n",nal_unit_type); + fprint_msg(" H.264 does not specify nal_unit_type %02X," + " so not H.264\n",nal_unit_type); *maybe_h264 = FALSE; } else if (nal_ref_idc == 0) @@ -1448,8 +1443,8 @@ static int try_to_guess_video_type(ES_unit_p unit, nal_unit_type == 8) // picture parameter set { if (show_reasoning) - printf(" H.264 does not allow nal_ref_idc 0 and nal_unit_type %d," - " so not H.264\n",nal_unit_type); + fprint_msg(" H.264 does not allow nal_ref_idc 0 and nal_unit_type %d," + " so not H.264\n",nal_unit_type); *maybe_h264 = FALSE; } } @@ -1463,8 +1458,8 @@ static int try_to_guess_video_type(ES_unit_p unit, nal_unit_type == 12) // fille { if (show_reasoning) - printf(" H.264 insists nal_ref_idc shall be 0 for nal_unit_type %d," - " so not H.264\n",nal_unit_type); + fprint_msg(" H.264 insists nal_ref_idc shall be 0 for nal_unit_type %d," + " so not H.264\n",nal_unit_type); *maybe_h264 = FALSE; } } @@ -1507,8 +1502,8 @@ extern int decide_ES_video_type(ES_p es, err = setup_ES_unit(&unit); if (err) { - fprintf(stderr,"### Error trying to setup ES unit before" - " working out video type\n"); + print_err("### Error trying to setup ES unit before" + " working out video type\n"); return 1; } @@ -1523,30 +1518,29 @@ extern int decide_ES_video_type(ES_p es, // *very* sure it is not H.262 or AVS. And if the only other choice is H.264, // then... if (show_reasoning) - printf("Looking through first 500 ES units to try to decide video type\n"); + print_msg("Looking through first 500 ES units to try to decide video type\n"); for (ii=0; ii<500; ii++) { if (print_dots) { - printf("."); + print_msg("."); fflush(stdout); } else if (show_reasoning) - printf("%d: ",ii+1); + fprint_msg("%d: ",ii+1); err = find_next_ES_unit(es,&unit); if (err == EOF) { - if (print_dots) printf("\n"); - if (show_reasoning) printf("End of file, trying to read ES unit %d\n",ii+2); + if (print_dots) print_msg("\n"); + if (show_reasoning) fprint_msg("End of file, trying to read ES unit %d\n",ii+2); break; } else if (err) { - if (print_dots) printf("\n"); - fprintf(stderr, - "### Error trying to find 'unit' %d in ES whilst" - " working out video type\n",ii+2); + if (print_dots) print_msg("\n"); + fprint_err("### Error trying to find 'unit' %d in ES whilst" + " working out video type\n",ii+2); clear_ES_unit(&unit); return 1; } @@ -1554,40 +1548,39 @@ extern int decide_ES_video_type(ES_p es, &maybe_h264,&maybe_h262,&maybe_avs); if (err) { - if (print_dots) printf("\n"); - fprintf(stderr, - "### Whilst trying to work out video_type\n"); + if (print_dots) print_msg("\n"); + print_err("### Whilst trying to work out video_type\n"); clear_ES_unit(&unit); return 1; } if (maybe_h264 && !maybe_h262 && !maybe_avs) { - if (show_reasoning) printf(" Which leaves only H.264\n"); + if (show_reasoning) print_msg(" Which leaves only H.264\n"); *video_type = VIDEO_H264; decided = TRUE; } else if (!maybe_h264 && maybe_h262 && !maybe_avs) { - if (show_reasoning) printf(" Which leaves only H.262\n"); + if (show_reasoning) print_msg(" Which leaves only H.262\n"); *video_type = VIDEO_H262; decided = TRUE; } else if (!maybe_h264 && !maybe_h262 && maybe_avs) { - if (show_reasoning) printf(" Which leaves only AVS\n"); + if (show_reasoning) print_msg(" Which leaves only AVS\n"); *video_type = VIDEO_AVS; decided = TRUE; } else { if (show_reasoning) - printf(" It is not possible to decide from that start code\n"); + print_msg(" It is not possible to decide from that start code\n"); } if (decided) break; } - if (print_dots) printf("\n"); + if (print_dots) print_msg("\n"); clear_ES_unit(&unit); return 0; } @@ -1623,31 +1616,30 @@ extern int decide_ES_file_video_type(int input, start_posn = tell_file(input); if (start_posn == -1) { - fprintf(stderr,"### Error remembering start position in file before" - " working out video type\n"); + print_err("### Error remembering start position in file before" + " working out video type\n"); return 1; } err = seek_file(input,0); if (err) { - fprintf(stderr,"### Error rewinding file before" - " working out video type\n"); + print_err("### Error rewinding file before working out video type\n"); return 1; } err = build_elementary_stream_file(input,&es); if (err) { - fprintf(stderr,"### Error starting elementary stream before" - " working out video type\n"); + print_err("### Error starting elementary stream before" + " working out video type\n"); return 1; } err = decide_ES_video_type(es,print_dots,show_reasoning,video_type); if (err) { - fprintf(stderr,"### Error deciding video type of file\n"); + print_err("### Error deciding video type of file\n"); free_elementary_stream(&es); return 1; } @@ -1657,8 +1649,8 @@ extern int decide_ES_file_video_type(int input, err = seek_file(input,start_posn); if (err) { - fprintf(stderr,"### Error returning to start position in file after" - " working out video type\n"); + print_err("### Error returning to start position in file after" + " working out video type\n"); return 1; } return 0; diff --git a/es2ts.c b/es2ts.c index aaad0c9..a3b0f9b 100644 --- a/es2ts.c +++ b/es2ts.c @@ -105,7 +105,7 @@ static int transfer_data(ES_p es, count++; if (verbose) - report_ES_unit(stderr,unit); + report_ES_unit(FALSE,unit); err = write_ES_unit_as_TS(output,unit,video_pid); if (err) diff --git a/es_fns.h b/es_fns.h index 899ee73..c9c446e 100644 --- a/es_fns.h +++ b/es_fns.h @@ -176,9 +176,9 @@ extern int build_ES_unit_from_data(ES_unit_p *unit, extern void free_ES_unit(ES_unit_p *unit); /* - * Print out some information this ES unit, on the given stream + * Print out some information this ES unit, on normal or error output */ -extern void report_ES_unit(FILE *stream, +extern void report_ES_unit(int is_msg, ES_unit_p unit); /* diff --git a/esreport.c b/esreport.c index 88d9105..94cc92e 100644 --- a/esreport.c +++ b/esreport.c @@ -247,7 +247,7 @@ static void report_ES_units(ES_p es, count++; if (!quiet) - report_ES_unit(stdout,&unit); + report_ES_unit(TRUE,&unit); if (verbose) print_data(TRUE," Data", diff --git a/h262.c b/h262.c index ab989fa..25baec9 100644 --- a/h262.c +++ b/h262.c @@ -44,7 +44,7 @@ /* - * Print out information derived from the start code, to the given stream. + * Print out information derived from the start code * * Note that if a "SYSTEM START" code is reported, then the data is * likely to be PES or Transport Stream data, not Elementary Stream. @@ -56,8 +56,7 @@ * that some of the apparent start code prefixes are actually false * detections. */ -extern void print_h262_start_code_str(FILE *stream, - byte start_code) +extern void print_h262_start_code_str(byte start_code) { byte number; char *str = NULL; @@ -98,25 +97,25 @@ extern void print_h262_start_code_str(FILE *stream, } if (str != NULL) - fprintf(stream,str); + print_msg(str); else if (start_code == 0x47) - fprintf(stream,"TRANSPORT STREAM sync byte"); + print_msg("TRANSPORT STREAM sync byte"); else if (start_code >= 0x01 && start_code <= 0xAF) - fprintf(stream,"Slice, vertical posn %d",start_code); + fprint_msg("Slice, vertical posn %d",start_code); else if (start_code >= 0xC0 && start_code <=0xDF) { number = start_code & 0x1F; - fprintf(stream,"SYSTEM START: Audio stream %02x",number); + fprint_msg("SYSTEM START: Audio stream %02x",number); } else if (start_code >= 0xE0 && start_code <= 0xEF) { number = start_code & 0x0F; - fprintf(stream,"SYSTEM START: Video stream %x",number); + fprint_msg("SYSTEM START: Video stream %x",number); } else if (start_code >= 0xFC && start_code <= 0xFE) - fprintf(stream,"SYSTEM START: Reserved data stream"); + print_msg("SYSTEM START: Reserved data stream"); else - fprintf(stream,"SYSTEM START: Unrecognised stream id"); + print_msg("SYSTEM START: Unrecognised stream id"); } /* @@ -130,13 +129,13 @@ extern int build_h262_item(h262_item_p *item) h262_item_p new = malloc(SIZEOF_H262_ITEM); if (new == NULL) { - fprintf(stderr,"### Unable to allocate MPEG2 item datastructure\n"); + print_err("### Unable to allocate MPEG2 item datastructure\n"); return 1; } err = setup_ES_unit(&(new->unit)); if (err) { - fprintf(stderr,"### Unable to allocate MPEG2 item data buffer\n"); + print_err("### Unable to allocate MPEG2 item data buffer\n"); free(new); return 1; } @@ -168,7 +167,7 @@ extern void report_h262_item(h262_item_p item) fprint_msg(OFFSET_T_FORMAT_08 "/%04d: MPEG2 item %02x (", item->unit.start_posn.infile, item->unit.start_posn.inpacket,item->unit.start_code); - print_h262_start_code_str(stdout,item->unit.start_code); // XXX Fix + print_h262_start_code_str(item->unit.start_code); print_msg(")"); if (item->unit.start_code == 0) fprint_msg(" %d (%s)",item->picture_coding_type, @@ -232,7 +231,7 @@ extern int build_h262_context(ES_p es, h262_context_p new = malloc(SIZEOF_H262_CONTEXT); if (new == NULL) { - fprintf(stderr,"### Unable to allocate H.262 context datastructure\n"); + print_err("### Unable to allocate H.262 context datastructure\n"); return 1; } @@ -355,14 +354,14 @@ static int build_h262_picture(h262_context_p context, h262_picture_p new = malloc(SIZEOF_H262_PICTURE); if (new == NULL) { - fprintf(stderr,"### Unable to allocate H.262 picture datastructure\n"); + print_err("### Unable to allocate H.262 picture datastructure\n"); return 1; } err = build_ES_unit_list(&(new->list)); if (err) { - fprintf(stderr,"### Unable to allocate internal list for H.262 picture\n"); + print_err("### Unable to allocate internal list for H.262 picture\n"); free(new); return 1; } @@ -401,9 +400,8 @@ static int build_h262_picture(h262_context_p context, } else { - fprintf(stderr, - "!!! Building H.262 picture that starts with a %s (%02x)\n", - H262_START_CODE_STR(item->unit.start_code),item->unit.start_code); + fprint_err("!!! Building H.262 picture that starts with a %s (%02x)\n", + H262_START_CODE_STR(item->unit.start_code),item->unit.start_code); new->is_picture = FALSE; new->is_sequence_header = FALSE; new->picture_coding_type = 0; // Forbidden value, just in case @@ -412,8 +410,8 @@ static int build_h262_picture(h262_context_p context, err = append_to_h262_picture(new,item); if (err) { - fprintf(stderr,"### Error appending first item to H.262 %s\n", - H262_START_CODE_STR(item->unit.start_code)); + fprint_err("### Error appending first item to H.262 %s\n", + H262_START_CODE_STR(item->unit.start_code)); free_h262_picture(&new); return 1; } @@ -439,7 +437,7 @@ static int append_fake_afd(h262_picture_p picture, err = build_h262_item(&item); if (err) { - fprintf(stderr,"### Error building 'fake' AFD for H.262 picture\n"); + print_err("### Error building 'fake' AFD for H.262 picture\n"); return 1; } item->unit.data[0] = 0x00; @@ -462,7 +460,7 @@ static int append_fake_afd(h262_picture_p picture, err = append_to_h262_picture(picture,item); if (err) { - fprintf(stderr,"### Error appending 'fake' AFD to H.262 picture\n"); + print_err("### Error appending 'fake' AFD to H.262 picture\n"); return 1; } @@ -490,7 +488,7 @@ static int merge_fields(h262_picture_p picture1, &picture2->list->array[ii]); if (err) { - fprintf(stderr,"### Error merging two H.262 field pictures\n"); + print_err("### Error merging two H.262 field pictures\n"); return 1; } } @@ -562,8 +560,7 @@ static int maybe_remember_this_picture(h262_context_p h262, err = get_ES_unit_list_bounds(this_picture->list,&start_posn,&num_bytes); if (err) { - fprintf(stderr, - "### Error working out position/size of H.262 picture\n"); + print_err("### Error working out position/size of H.262 picture\n"); return 1; } @@ -573,14 +570,13 @@ static int maybe_remember_this_picture(h262_context_p h262, this_picture->afd); if (err) { - fprintf(stderr, - "### Error remembering reversing data for H.262 item\n"); + print_err("### Error remembering reversing data for H.262 item\n"); return 1; } if (verbose) - printf("REMEMBER I picture %5d at " OFFSET_T_FORMAT_08 - "/%04d for %5d\n",h262->picture_index, - start_posn.infile,start_posn.inpacket,num_bytes); + fprint_msg("REMEMBER I picture %5d at " OFFSET_T_FORMAT_08 + "/%04d for %5d\n",h262->picture_index, + start_posn.infile,start_posn.inpacket,num_bytes); } } else if (this_picture->is_sequence_header) @@ -590,21 +586,21 @@ static int maybe_remember_this_picture(h262_context_p h262, err = get_ES_unit_list_bounds(this_picture->list,&start_posn,&num_bytes); if (err) { - fprintf(stderr,"### Error working out position/size of H.262" - " sequence header for reversing data\n"); + print_err("### Error working out position/size of H.262" + " sequence header for reversing data\n"); return 1; } err = remember_reverse_h262_data(h262->reverse_data,0, start_posn,num_bytes,0,0); if (err) { - fprintf(stderr,"### Error remembering reversing data for H.262 item\n"); + print_err("### Error remembering reversing data for H.262 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); + fprint_msg("REMEMBER Sequence header at " OFFSET_T_FORMAT_08 + "/%04d for %5d\n", + start_posn.infile,start_posn.inpacket,num_bytes); } return 0; } @@ -628,16 +624,16 @@ static int extract_AFD(h262_item_p item, // AFD flag set if (item->unit.data_len < 10) { - fprintf(stderr,"!!! AFD too short (only %d bytes - AFD missing)\n", - item->unit.data_len); + fprint_err("!!! AFD too short (only %d bytes - AFD missing)\n", + item->unit.data_len); *afd = UNSET_AFD_BYTE; return 1; } *afd = item->unit.data[9]; if ((item->unit.data[9] & 0xF0) != 0xF0) { - fprintf(stderr,"### Bad AFD %02x (reserved bits not 1111)\n", - item->unit.data[9]); + fprint_err("### Bad AFD %02x (reserved bits not 1111)\n", + item->unit.data[9]); return 1; } } @@ -647,8 +643,8 @@ static int extract_AFD(h262_item_p item, } else { - fprintf(stderr,"### AFD datastructure malformed: flag byte is %02x" - " instead of 0x41 or 0x01\n",item->unit.data[8]); + fprint_err("### AFD datastructure malformed: flag byte is %02x" + " instead of 0x41 or 0x01\n",item->unit.data[8]); if (item->unit.data_len == 9) *afd = UNSET_AFD_BYTE; else @@ -664,21 +660,21 @@ static int extract_AFD(h262_item_p item, */ static void _show_item(h262_item_p item) { - printf("__ "); + print_msg("__ "); if (item == NULL) { - printf("\n"); + print_msg("\n"); return; } if (is_h262_picture_item(item)) - printf("%s picture",H262_PICTURE_CODING_STR(item->picture_coding_type)); + fprint_msg("%s picture",H262_PICTURE_CODING_STR(item->picture_coding_type)); else if (is_h262_slice_item(item)) - printf("slice %2x",item->unit.start_code); + fprint_msg("slice %2x",item->unit.start_code); else - printf("%s",H262_START_CODE_STR(item->unit.start_code)); - printf(" at " OFFSET_T_FORMAT "/%d for %d\n", - item->unit.start_posn.infile,item->unit.start_posn.inpacket, - item->unit.data_len); + fprint_msg("%s",H262_START_CODE_STR(item->unit.start_code)); + fprint_msg(" at " OFFSET_T_FORMAT "/%d for %d\n", + item->unit.start_posn.infile,item->unit.start_posn.inpacket, + item->unit.data_len); } #endif @@ -718,7 +714,7 @@ extern int get_next_h262_single_picture(h262_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; @@ -756,7 +752,7 @@ extern int get_next_h262_single_picture(h262_context_p context, #if DEBUG_GET_NEXT_PICTURE if (verbose) { - printf("__ --------------------------------- \n"); + print_msg("__ --------------------------------- \n"); _show_item(item); } #endif @@ -771,7 +767,7 @@ extern int get_next_h262_single_picture(h262_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; } @@ -814,14 +810,14 @@ extern int get_next_h262_single_picture(h262_context_p context, // We found a *real* AFD - remember it err = extract_AFD(item,&(*picture)->afd); if (err) - fprintf(stderr,"!!! Assuming AFD %x at " OFFSET_T_FORMAT "/%d\n", - (*picture)->afd, - item->unit.start_posn.infile,item->unit.start_posn.inpacket); + fprint_err("!!! Assuming AFD %x at " OFFSET_T_FORMAT "/%d\n", + (*picture)->afd, + item->unit.start_posn.infile,item->unit.start_posn.inpacket); (*picture)->is_real_afd = TRUE; #if DEBUG_AFD if ((*picture)->afd != context->last_afd) { - printf("* "); + print_msg("* "); report_h262_picture(stdout,*picture,FALSE); } #endif @@ -843,10 +839,10 @@ extern int get_next_h262_single_picture(h262_context_p context, #if DEBUG_GET_NEXT_PICTURE if (verbose) { - printf("__ fake AFD "); + print_msg("__ fake AFD "); print_bits(4,(*picture)->afd); - printf(", i.e., %s",SHORT_AFD_STR((*picture)->afd)); - printf("\n"); + fprint_msg(", i.e., %s",SHORT_AFD_STR((*picture)->afd)); + print_msg("\n"); } #endif } @@ -871,7 +867,7 @@ extern int get_next_h262_single_picture(h262_context_p context, err = append_to_h262_picture(*picture,item); if (err) { - fprintf(stderr,"### Error adding item to H.262 sequence header\n"); + print_err("### Error adding item to H.262 sequence header\n"); free_h262_picture(picture); return 1; } @@ -892,15 +888,15 @@ extern int get_next_h262_single_picture(h262_context_p context, if (num_slices > 1) { ES_unit_p unit = &(*picture)->list->array[(*picture)->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_picture || in_sequence_header) _show_item(item); } @@ -936,15 +932,15 @@ static int get_next_field_of_pair(h262_context_p context, h262_picture_p second; if (verbose) - 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_h262_single_picture(context,verbose,&second); if (err) { if (err != EOF) - fprintf(stderr,"### Trying to read second field\n"); + print_err("### Trying to read second field\n"); return err; } @@ -952,8 +948,8 @@ static int get_next_field_of_pair(h262_context_p context, { // But it was either a frame or a sequence header - oh dear if (!quiet) - fprintf(stderr,"!!! Field followed by a %s - ignoring the field\n", - (second->is_picture?"frame":"sequence header")); + fprint_err("!!! Field followed by a %s - ignoring the field\n", + (second->is_picture?"frame":"sequence header")); free_h262_picture(picture); *picture = second; // and pretend to success @@ -961,7 +957,7 @@ static int get_next_field_of_pair(h262_context_p context, else if ((*picture)->temporal_reference == second->temporal_reference) { // They appear to be matching fields - make a frame from them - if (verbose) printf("@@ Merging two fields\n"); + if (verbose) print_msg("@@ Merging two fields\n"); err = merge_fields(*picture,second); if (err) { @@ -973,10 +969,10 @@ static int get_next_field_of_pair(h262_context_p context, else if (first_time) { if (!quiet) - fprintf(stderr,"!!! Field with temporal ref %d (%x) followed by" - " field with temporal ref %d (%x) - ignoring first field\n", - (*picture)->temporal_reference,(*picture)->temporal_reference, - second->temporal_reference,second->temporal_reference); + fprint_err("!!! Field with temporal ref %d (%x) followed by" + " field with temporal ref %d (%x) - ignoring first field\n", + (*picture)->temporal_reference,(*picture)->temporal_reference, + second->temporal_reference,second->temporal_reference); // Try again free_h262_picture(picture); @@ -986,8 +982,8 @@ static int get_next_field_of_pair(h262_context_p context, } else { - fprintf(stderr,"### Adjacent fields do not share temporal references" - " - unable to match fields up\n"); + print_err("### Adjacent fields do not share temporal references" + " - unable to match fields up\n"); return 1; } return 0; @@ -1100,7 +1096,7 @@ extern int write_h262_picture_as_TS(TS_writer_p tswriter, DEFAULT_VIDEO_STREAM_ID); if (err) { - fprintf(stderr,"### Error writing out picture list to TS\n"); + print_err("### Error writing out picture list to TS\n"); return err; } } @@ -1133,7 +1129,7 @@ extern int write_h262_picture_as_ES(FILE *output, err = write_ES_unit(output,unit); if (err) { - fprintf(stderr,"### Error writing out picture list to ES\n"); + print_err("### Error writing out picture list to ES\n"); return err; } } @@ -1152,44 +1148,44 @@ extern void report_h262_picture(h262_picture_p picture, { if (picture->is_picture) { - printf("%s %s #%02d", - H262_PICTURE_CODING_STR(picture->picture_coding_type), - H262_PICTURE_STRUCTURE_STR(picture->picture_structure), - picture->temporal_reference); + fprint_msg("%s %s #%02d", + H262_PICTURE_CODING_STR(picture->picture_coding_type), + H262_PICTURE_STRUCTURE_STR(picture->picture_structure), + picture->temporal_reference); if (picture->was_two_fields) - printf(" (merged)"); + print_msg(" (merged)"); - printf(" %s",H262_ASPECT_RATIO_INFO_STR(picture->aspect_ratio_info)); + fprint_msg(" %s",H262_ASPECT_RATIO_INFO_STR(picture->aspect_ratio_info)); if (picture->is_real_afd) - printf(" AFD "); + print_msg(" AFD "); else - printf(" afd "); + print_msg(" afd "); print_bits(4,picture->afd); - printf(", i.e., %s",SHORT_AFD_STR(picture->afd)); - printf("\n"); + fprint_msg(", i.e., %s",SHORT_AFD_STR(picture->afd)); + print_msg("\n"); } else if (picture->is_sequence_header) { - printf("Sequence header: "); + print_msg("Sequence header: "); switch (picture->progressive_sequence) { - case 0: printf("frames and fields"); break; - case 1: printf("progressive frames only"); break; + case 0: print_msg("frames and fields"); break; + case 1: print_msg("progressive frames only"); break; default: - printf("progressive_sequence=%d", - picture->progressive_sequence); + fprint_msg("progressive_sequence=%d", + picture->progressive_sequence); break; } - printf(", aspect ratio %s", - H262_ASPECT_RATIO_INFO_STR(picture->aspect_ratio_info)); - printf("\n"); + fprint_msg(", aspect ratio %s", + H262_ASPECT_RATIO_INFO_STR(picture->aspect_ratio_info)); + print_msg("\n"); } else { - printf("Sequence end\n"); + print_msg("Sequence end\n"); } if (report_data) report_ES_unit_list("ES units",picture->list); diff --git a/h262_fns.h b/h262_fns.h index f0713c4..66be790 100644 --- a/h262_fns.h +++ b/h262_fns.h @@ -32,7 +32,7 @@ #include "h262_defns.h" /* - * Print out information derived from the start code, to the given stream. + * Print out information derived from the start code * * Note that if a "SYSTEM START" code is reported, then the data is * likely to be PES or Transport Stream data, not Elementary Stream. @@ -44,8 +44,7 @@ * that some of the apparent start code prefixes are actually false * detections. */ -extern void print_h262_start_code_str(FILE *stream, - byte start_code); +extern void print_h262_start_code_str(byte start_code); /* * Build a new MPEG2 item datastructure. * diff --git a/pes.c b/pes.c index 97d6f89..2c1ae49 100644 --- a/pes.c +++ b/pes.c @@ -3056,7 +3056,7 @@ extern void report_PES_data_array2(int stream_type, printf(" Start code: %02x %02x %02x\n", payload[0],payload[1],payload[2]); printf(" Stream ID: %02x (%d) ",stream_id,stream_id); - print_h262_start_code_str(stdout,stream_id); + print_h262_start_code_str(stream_id); printf("\n"); printf(" PES packet length: %04x (%d)\n", PES_packet_length,PES_packet_length);