// Buffer caption blocks for later sorting/flushing. void store_hdcc(unsigned char *cc_data, int cc_count, int sequence_number, LLONG current_fts_now) { // Uninitialized? if (anchor_seq_number < 0) { anchor_hdcc( sequence_number); } int seq_index = sequence_number - anchor_seq_number + MAXBFRAMES; if (seq_index < 0 || seq_index > 2*MAXBFRAMES) { // Maybe missing an anchor frame - try to recover dbg_print(CCX_DMT_VERBOSE, "Too many B-frames, or missing anchor frame. Trying to recover ..\n"); process_hdcc(); anchor_hdcc( sequence_number); seq_index = sequence_number - anchor_seq_number + MAXBFRAMES; } has_ccdata_buffered = 1; // In GOP mode the fts is set only once for the whole GOP. Recreate // the right time according to the sequence number. if (ccx_options.use_gop_as_pts==1) { current_fts_now += (LLONG) (sequence_number*1000.0/current_fps); } if (cc_count) { if (cc_data) { // Changed by CFS to concat, i.e. don't assume there's no data already for this seq_index. // Needed at least for MP4 samples. // TODO: make sure we don't overflow cc_fts[seq_index] = current_fts_now; // CFS: Maybe do even if there's no data? if (stream_mode!=CCX_SM_MP4) // CFS: Very ugly hack, but looks like overwriting is needed for at least some ES cc_data_count[seq_index] = 0; memcpy(cc_data_pkts[seq_index]+cc_data_count[seq_index]*3, cc_data, cc_count*3+1); } cc_data_count[seq_index] += cc_count; } // DEBUG STUFF /* printf("\nCC blocks, channel 0:\n"); for ( int i=0; i < cc_count*3; i+=3) { printf("%s", debug_608toASC( cc_data+i, 0) ); } printf("\n"); */ }
// Buffer caption blocks for later sorting/flushing. void store_hdcc(unsigned char *cc_data, int cc_count, int sequence_number, LLONG current_fts_now) { // Uninitialized? if (anchor_seq_number < 0) { anchor_hdcc( sequence_number); } int seq_index = sequence_number - anchor_seq_number + MAXBFRAMES; if (seq_index < 0 || seq_index > 2*MAXBFRAMES) { // Maybe missing an anchor frame - try to recover if(debug_verbose) printf("Too many B-frames, or missing anchor frame. Trying to recover ..\n"); process_hdcc(); anchor_hdcc( sequence_number); seq_index = sequence_number - anchor_seq_number + MAXBFRAMES; } has_ccdata_buffered = 1; cc_data_count[seq_index] = cc_count; // In GOP mode the fts is set only once for the whole GOP. Recreate // the right time according to the sequence number. if (use_gop_as_pts) { current_fts_now += LLONG(sequence_number*1000.0/current_fps); } cc_fts[seq_index] = current_fts_now; memcpy(cc_data_pkts[seq_index], cc_data, cc_count*3+1); // DEBUG STUFF /* printf("\nCC blocks, channel 0:\n"); for ( int i=0; i < cc_count*3; i+=3) { printf("%s", debug_608toASC( cc_data+i, 0) ); } printf("\n"); */ }
// Return TRUE if all was read. FALSE if a problem occured: // If a bitstream syntax problem occured the bitstream will // point to after the problem, in case we run out of data the bitstream // will point to where we want to restart after getting more. static int read_pic_info(struct bitstream *esstream) { dbg_print(DMT_VERBOSE, "Read PIC Info\n"); // We only get here after seeing that start code if (next_u32(esstream) != 0x00010000) // LSB first (0x00000100) fatal(EXIT_BUG_BUG, "Impossible!"); // If we get here esstream points to the start of a group_start_code // should we run out of data in esstream this is where we want to restart // after getting more. unsigned char *pic_info_start = esstream->pos; pic_header(esstream); pic_coding_ext(esstream); if (esstream->error) return 0; if (esstream->bitsleft < 0) { init_bitstream(esstream, pic_info_start, esstream->end); return 0; } // Analyse/use the picture information static int maxtref; // Use to remember the temporal reference number // A new anchor frame - flush buffered caption data. Might be flushed // in GOP header already. if (picture_coding_type==I_FRAME || picture_coding_type==P_FRAME) { // if (((picture_structure != 0x1) && (picture_structure != 0x2)) || // (temporal_reference != current_tref)) // { // NOTE: process_hdcc() needs to be called before set_fts() as it // uses fts_now to re-create the timeline !!!!! if (has_ccdata_buffered) { process_hdcc(); } anchor_hdcc(temporal_reference); // } } current_tref = temporal_reference; current_picture_coding_type = picture_coding_type; // We mostly use PTS, but when the GOP mode is enabled do not set // the FTS time here. if (!use_gop_as_pts) { set_fts(); // Initialize fts } dbg_print(DMT_VIDES, "PTS: %s (%8u) - tref: %2d - %s since tref0/GOP: %2u/%2u", print_mstime(current_pts/(MPEG_CLOCK_FREQ/1000)), unsigned(current_pts), temporal_reference, pict_types[picture_coding_type], unsigned(frames_since_ref_time), unsigned(frames_since_last_gop)); dbg_print(DMT_VIDES, " t:%d r:%d p:%d", top_field_first, repeat_first_field, progressive_frame); dbg_print(DMT_VIDES, " FTS: %s\n", print_mstime(get_fts())); // Set min_pts/sync_pts according to the current time stamp. // Use fts_at_gop_start as reference when a GOP header was seen // since the last frame 0. If not this is most probably a // TS without GOP headers but with USER DATA after each picture // header. Use the current FTS values as reference. // Note: If a GOP header was present the reference time is from // the beginning of the GOP, otherwise it is now. if(temporal_reference == 0) { last_gop_length = maxtref + 1; maxtref = temporal_reference; // frames_since_ref_time is used in set_fts() if( saw_gop_header ) { // This time (fts_at_gop_start) that was set in the // GOP header and it might be off by one GOP. See the comment there. frames_since_ref_time = frames_since_last_gop; // Should this be 0? } else { // No GOP header, use the current values fts_at_gop_start=get_fts(); frames_since_ref_time = 0; } if (debug_mask & DMT_TIME) { dbg_print(DMT_TIME, "\nNew temporal reference:\n"); print_debug_timing(); } saw_gop_header = 0; // Reset the value } if ( !saw_gop_header && picture_coding_type==I_FRAME ) { // A new GOP beginns with an I-frame. Lets hope there are // never more than one per GOP frames_since_last_gop = 0; } // Set maxtref if( temporal_reference > maxtref ) { maxtref = temporal_reference; if (maxtref+1 > max_gop_length) max_gop_length = maxtref+1; } unsigned extraframe = 0; if (repeat_first_field) { pulldownfields++; total_pulldownfields++; if ( current_progressive_sequence || !(total_pulldownfields%2) ) extraframe = 1; if ( current_progressive_sequence && top_field_first ) extraframe = 2; dbg_print(DMT_VIDES, "Pulldown: total pd fields: %d - %d extra frames\n", total_pulldownfields, extraframe); } total_pulldownframes += extraframe; total_frames_count += 1+extraframe; frames_since_last_gop += 1+extraframe; frames_since_ref_time += 1+extraframe; dbg_print(DMT_VERBOSE, "Read PIC Info - processed\n\n"); return 1; }
// Return TRUE if the data parsing finished, FALSE otherwise. // estream->pos is advanced. Data is only processed if esstream->error // is FALSE, parsing can set esstream->error to TRUE. static int gop_header(struct bitstream *esstream) { dbg_print(DMT_VERBOSE, "GOP header\n"); if (esstream->error || esstream->bitsleft <= 0) return 0; // We only get here after seeing that start code if (read_u32(esstream) != 0xB8010000) // LSB first (0x000001B8) fatal(EXIT_BUG_BUG, "Impossible!"); unsigned drop_frame_flag = (unsigned) read_bits(esstream,1); struct gop_time_code gtc; gtc.time_code_hours = (int) read_bits(esstream,5); gtc.time_code_minutes = (int) read_bits(esstream,6); skip_bits(esstream,1); // Marker bit gtc.time_code_seconds = (int) read_bits(esstream,6); gtc.time_code_pictures = (int) read_bits(esstream,6); gtc.inited = 1; calculate_ms_gop_time(>c); if (esstream->bitsleft < 0) return 0; if (gop_accepted(>c)) { // Do GOP padding during GOP header. The previous GOP and all // included captions are written. Use the current GOP time to // do the padding. // Flush buffered cc blocks before doing the housekeeping if (has_ccdata_buffered) { process_hdcc(); } // Last GOPs pulldown frames if ((current_pulldownfields>0) != (pulldownfields>0)) { current_pulldownfields = pulldownfields; dbg_print(DMT_VERBOSE, "Pulldown: %s", (pulldownfields ? "on" : "off")); if (pulldownfields) dbg_print(DMT_VERBOSE, " - %u fields in last GOP", pulldownfields); dbg_print(DMT_VERBOSE, "\n"); } pulldownfields = 0; // Report synchronization jumps between GOPs. Warn if there // are 20% or more deviation. if ( (debug_mask & DMT_TIME) && ((gtc.ms - gop_time.ms // more than 20% longer > frames_since_last_gop*1000.0/current_fps*1.2) || (gtc.ms - gop_time.ms // or 20% shorter < frames_since_last_gop*1000.0/current_fps*0.8)) && first_gop_time.inited ) { mprint("\rWarning: Jump in GOP timing.\n"); mprint(" (old) %s", print_mstime(gop_time.ms)); mprint(" + %s (%uF)", print_mstime(LLONG(frames_since_last_gop *1000.0/current_fps)), frames_since_last_gop); mprint(" != (new) %s\n", print_mstime(gtc.ms)); } if (first_gop_time.inited == 0) { first_gop_time = gtc; // It needs to be "+1" because the frame count starts at 0 and we // need the length of all frames. if ( total_frames_count == 0 ) { // If this is the first frame there cannot be an offset fts_fc_offset = 0; // first_gop_time.ms stays unchanged } else { fts_fc_offset = LLONG((total_frames_count+1) *1000.0/current_fps); // Compensate for those written before first_gop_time.ms -= fts_fc_offset; } dbg_print(DMT_TIME, "\nFirst GOP time: %02u:%02u:%02u:%03u %+lldms\n", gtc.time_code_hours, gtc.time_code_minutes, gtc.time_code_seconds, unsigned(1000.0*gtc.time_code_pictures/current_fps), fts_fc_offset); } gop_time = gtc; frames_since_last_gop=0; // Indicate that we read a gop header (since last frame number 0) saw_gop_header = 1; // If we use GOP timing, reconstruct the PTS from the GOP if (use_gop_as_pts) { current_pts = gtc.ms*(MPEG_CLOCK_FREQ/1000); if (pts_set==0) pts_set=1; current_tref = 0; frames_since_ref_time = 0; set_fts(); fts_at_gop_start = get_fts_max(); } else { // FIXME: Wrong when PTS are not increasing but are identical // troughout the GOP and then jump to the next time for the // next GOP. // This effect will also lead to captions being one GOP early // for DVD captions. fts_at_gop_start = get_fts_max() + LLONG(1000.0/current_fps); } if (debug_mask & DMT_TIME) { dbg_print(DMT_TIME, "\nNew GOP:\n"); dbg_print(DMT_TIME, "\nDrop frame flag: %u:\n", drop_frame_flag); print_debug_timing(); } } return 1; }
// Return TRUE if all was read. FALSE if a problem occured: // If a bitstream syntax problem occured the bitstream will // point to after the problem, in case we run out of data the bitstream // will point to where we want to restart after getting more. static int read_pic_info(struct lib_cc_decode *ctx, struct bitstream *esstream, struct cc_subtitle *sub) { debug("Read PIC Info\n"); // We only get here after seeing that start code if (next_u32(esstream) != 0x00010000) // LSB first (0x00000100) fatal(CCX_COMMON_EXIT_BUG_BUG, "In read_pic_info: next_u32(esstream) != 0x00010000. Please file a bug report in GitHub.\n"); // If we get here esstream points to the start of a group_start_code // should we run out of data in esstream this is where we want to restart // after getting more. unsigned char *pic_info_start = esstream->pos; pic_header(ctx, esstream); pic_coding_ext(ctx, esstream); if (esstream->error) return 0; if (esstream->bitsleft < 0) { init_bitstream(esstream, pic_info_start, esstream->end); return 0; } // A new anchor frame - flush buffered caption data. Might be flushed // in GOP header already. if (ctx->picture_coding_type==CCX_FRAME_TYPE_I_FRAME || ctx->picture_coding_type==CCX_FRAME_TYPE_P_FRAME) { if (((ctx->picture_structure != 0x1) && (ctx->picture_structure != 0x2)) || (ctx->temporal_reference != ctx->timing->current_tref)) { // NOTE: process_hdcc() needs to be called before set_fts() as it // uses fts_now to re-create the timeline !!!!! if (ctx->has_ccdata_buffered) { process_hdcc(ctx, sub); } anchor_hdcc(ctx, ctx->temporal_reference); } } ctx->timing->current_tref = ctx->temporal_reference; ctx->timing->current_picture_coding_type = ctx->picture_coding_type; // We mostly use PTS, but when the GOP mode is enabled do not set // the FTS time here. if (ccx_options.use_gop_as_pts!=1) { set_fts(ctx->timing); // Initialize fts } dbg_print(CCX_DMT_VIDES, " t:%d r:%d p:%d", ctx->top_field_first, ctx->repeat_first_field, ctx->progressive_frame); dbg_print(CCX_DMT_VIDES, " FTS: %s\n", print_mstime_static(get_fts(ctx->timing, ctx->current_field))); // Set min_pts/sync_pts according to the current time stamp. // Use fts_at_gop_start as reference when a GOP header was seen // since the last frame 0. If not this is most probably a // TS without GOP headers but with USER DATA after each picture // header. Use the current FTS values as reference. // Note: If a GOP header was present the reference time is from // the beginning of the GOP, otherwise it is now. if(ctx->temporal_reference == 0) { ctx->last_gop_length = ctx->maxtref + 1; ctx->maxtref = ctx->temporal_reference; // frames_since_ref_time is used in set_fts() if( ctx->saw_gop_header ) { // This time (fts_at_gop_start) that was set in the // GOP header and it might be off by one GOP. See the comment there. frames_since_ref_time = ctx->frames_since_last_gop; // Should this be 0? } else { // No GOP header, use the current values fts_at_gop_start = get_fts(ctx->timing, ctx->current_field); frames_since_ref_time = 0; } if (ccx_options.debug_mask & CCX_DMT_TIME) { dbg_print(CCX_DMT_TIME, "\nNew temporal reference:\n"); print_debug_timing(ctx->timing); } ctx->saw_gop_header = 0; // Reset the value } if ( !ctx->saw_gop_header && ctx->picture_coding_type==CCX_FRAME_TYPE_I_FRAME ) { // A new GOP beginns with an I-frame. Lets hope there are // never more than one per GOP ctx->frames_since_last_gop = 0; } // Set maxtref if( ctx->temporal_reference > ctx->maxtref ) { ctx->maxtref = ctx->temporal_reference; if (ctx->maxtref + 1 > ctx->max_gop_length) ctx->max_gop_length = ctx->maxtref + 1; } unsigned extraframe = 0; if (ctx->repeat_first_field) { ctx->pulldownfields++; ctx->total_pulldownfields++; if ( ctx->current_progressive_sequence || !(ctx->total_pulldownfields%2) ) extraframe = 1; if ( ctx->current_progressive_sequence && ctx->top_field_first ) extraframe = 2; dbg_print(CCX_DMT_VIDES, "Pulldown: total pd fields: %d - %d extra frames\n", ctx->total_pulldownfields, extraframe); } ctx->total_pulldownframes += extraframe; total_frames_count += 1+extraframe; ctx->frames_since_last_gop += 1+extraframe; frames_since_ref_time += 1+extraframe; debug("Read PIC Info - processed\n\n"); return 1; }