From cc1922107a07f79a1285dba3a21ec413b92606bc Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Fri, 27 Sep 2024 10:46:27 +0200 Subject: [PATCH 1/4] Update cut_bs.py such that --frame and --length may be combined --- scripts/cut_bs.py | 64 ++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/scripts/cut_bs.py b/scripts/cut_bs.py index 37f19733df..8c7ff38c71 100755 --- a/scripts/cut_bs.py +++ b/scripts/cut_bs.py @@ -59,45 +59,42 @@ def cut_to_length(fp, fp_out, length): return fr_cnt - -def cut_from_start(fp, fp_out, start_frame=0, start_with_sid=False): +def cut_from_start(fp, fp_out, start_frame=0, length=-1, start_with_sid=False): # cut until start frame fr_cnt = 0 - cut_cnt = 0 + extracted_frames_cnt = 0 for cur_frame in range(start_frame): sync_word = fp.read(2) if sync_word == b"": - return (fr_cnt, cut_cnt) + return (fr_cnt, extracted_frames_cnt) if sync_word not in SYNC_WORDS: raise ValueError("Bad Sync word!") n_bits = struct.unpack("h", fp.read(2))[0] fp.read(n_bits * 2) fr_cnt += 1 - cut_cnt += 1 if start_with_sid: found = False while not found: sync_word = fp.read(2) if sync_word == b"": - return (fr_cnt, cut_cnt) + return (fr_cnt, extracted_frames_cnt) if sync_word not in SYNC_WORDS: raise ValueError("Bad Sync word!") n_bits_bs = fp.read(2) n_bits = struct.unpack("h", n_bits_bs)[0] if n_bits in SID_BITS: found = True - fp_out.write(sync_word) - fp_out.write(n_bits_bs) - fp_out.write(fp.read(n_bits * 2)) + fp.seek(-4,1) # Found SID frame, go back else: fp.read(n_bits * 2) - cut_cnt += 1 - fr_cnt += 1 + fr_cnt += 1 - # transfer remaining frames - while True: + # transfer frames + eof = False + while length != 0: sync_word = fp.read(2) if sync_word == b"": + eof = True break if sync_word not in SYNC_WORDS: raise ValueError("Bad Sync word!") @@ -107,7 +104,23 @@ def cut_from_start(fp, fp_out, start_frame=0, start_with_sid=False): fp_out.write(n_bits_bs) fp_out.write(fp.read(n_bits * 2)) fr_cnt += 1 - return (fr_cnt, cut_cnt) + extracted_frames_cnt += 1 + length -= 1 + + # count remaining frames, if any + while not eof: + sync_word = fp.read(2) + if sync_word == b"": + eof = True + break + if sync_word not in SYNC_WORDS: + raise ValueError("Bad Sync word!") + n_bits_bs = fp.read(2) + n_bits = struct.unpack("h", n_bits_bs)[0] + fp.read(n_bits * 2) + fr_cnt += 1 + + return (fr_cnt, extracted_frames_cnt) if __name__ == "__main__": @@ -145,17 +158,12 @@ if __name__ == "__main__": with open(my_args.bs_in, "rb") as fp_in: with open(my_args.bs_out, "wb") as fp_out: - if my_args.sid or my_args.frame: - fr_cnt, cut_cnt = cut_from_start( - fp_in, fp_out, start_frame=my_args.frame, start_with_sid=my_args.sid - ) - if my_args.sid and (fr_cnt == cut_cnt): - print("Warning! No SID frame found in bitstream!") - print(f"Cut {cut_cnt} of {fr_cnt} frames from {my_args.bs_in}") - elif my_args.length: - fr_cnt = cut_to_length(fp_in, fp_out, my_args.length) - if fr_cnt != my_args.length: - print( - f"Warning! Could not cut to length {my_args.length} as bitstream only contained {fr_cnt} frames!" - ) - print(f"Cut {my_args.bs_in} to {fr_cnt} frames") + fr_cnt, extracted_frames_cnt = cut_from_start( + fp_in, fp_out, start_frame=my_args.frame, length=my_args.length, start_with_sid=my_args.sid + ) + if extracted_frames_cnt == 0: + print(f"\nExtracted 0 frames!") + sys.exit(-1) + if (my_args.length != -1 and extracted_frames_cnt != my_args.length): + print(f"Warning! Requested {my_args.length}, only {extracted_frames_cnt} obtained!") + print(f"Extracted {extracted_frames_cnt} out of {fr_cnt} frames from {my_args.bs_in}") -- GitLab From f3673e805c24ca8c29eb1f47a29b9b2aa66e9da3 Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Fri, 27 Sep 2024 10:57:55 +0200 Subject: [PATCH 2/4] Removed obsolete code in cut_bs.py --- scripts/cut_bs.py | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/scripts/cut_bs.py b/scripts/cut_bs.py index 8c7ff38c71..abbf506315 100755 --- a/scripts/cut_bs.py +++ b/scripts/cut_bs.py @@ -38,27 +38,6 @@ import sys SID_BITS = {35, 48, 104} SYNC_WORDS = {b"!k", b" k"} - -def cut_to_length(fp, fp_out, length): - assert length > 0 - - fr_cnt = 0 - - for f in range(length): - sync_word = fp.read(2) - if sync_word == b"": - return fr_cnt - if sync_word not in SYNC_WORDS: - raise ValueError("Bad Sync word!") - n_bits_bs = fp.read(2) - n_bits = struct.unpack("h", n_bits_bs)[0] - fp_out.write(sync_word) - fp_out.write(n_bits_bs) - fp_out.write(fp.read(n_bits * 2)) - fr_cnt += 1 - - return fr_cnt - def cut_from_start(fp, fp_out, start_frame=0, length=-1, start_with_sid=False): # cut until start frame fr_cnt = 0 -- GitLab From 7d16ead201069368330d6e58d6899dd6de694560 Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Mon, 30 Sep 2024 07:54:44 +0200 Subject: [PATCH 3/4] Clean up cut_bs.py --- scripts/cut_bs.py | 117 +++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 58 deletions(-) diff --git a/scripts/cut_bs.py b/scripts/cut_bs.py index abbf506315..fe855846af 100755 --- a/scripts/cut_bs.py +++ b/scripts/cut_bs.py @@ -38,66 +38,61 @@ import sys SID_BITS = {35, 48, 104} SYNC_WORDS = {b"!k", b" k"} + +def get_next_frame(fp): + eof = False + n_bits = 0 + data = None + sync_word = fp.read(2) + if sync_word == b"": + eof = True + return (sync_word, n_bits, data, eof) + if sync_word not in SYNC_WORDS: + raise ValueError("Bad Sync word!") + n_bits = struct.unpack("h", fp.read(2))[0] + data = fp.read(n_bits * 2) + return (sync_word, n_bits, data, eof) + + +def write_frame(fp_out, sync_word, n_bits, data): + fp_out.write(sync_word) + fp_out.write(struct.pack("h", n_bits)) + fp_out.write(data) + + def cut_from_start(fp, fp_out, start_frame=0, length=-1, start_with_sid=False): - # cut until start frame fr_cnt = 0 extracted_frames_cnt = 0 - for cur_frame in range(start_frame): - sync_word = fp.read(2) - if sync_word == b"": - return (fr_cnt, extracted_frames_cnt) - if sync_word not in SYNC_WORDS: - raise ValueError("Bad Sync word!") - n_bits = struct.unpack("h", fp.read(2))[0] - fp.read(n_bits * 2) - fr_cnt += 1 + eof = False + # cut until start frame + while fr_cnt < start_frame and not eof: + (sync_word, n_bits, data, eof) = get_next_frame(fp) + if not eof: + fr_cnt += 1 + # cut until SID frame if start_with_sid: found = False - while not found: - sync_word = fp.read(2) - if sync_word == b"": - return (fr_cnt, extracted_frames_cnt) - if sync_word not in SYNC_WORDS: - raise ValueError("Bad Sync word!") - n_bits_bs = fp.read(2) - n_bits = struct.unpack("h", n_bits_bs)[0] - if n_bits in SID_BITS: - found = True - fp.seek(-4,1) # Found SID frame, go back - else: - fp.read(n_bits * 2) + while not found and not eof: + (sync_word, n_bits, data, eof) = get_next_frame(fp) + if not eof: fr_cnt += 1 - + if n_bits in SID_BITS: + found = True + fp.seek(-(4 + n_bits * 2), 1) # Found SID, rewind # transfer frames - eof = False - while length != 0: - sync_word = fp.read(2) - if sync_word == b"": - eof = True - break - if sync_word not in SYNC_WORDS: - raise ValueError("Bad Sync word!") - n_bits_bs = fp.read(2) - n_bits = struct.unpack("h", n_bits_bs)[0] - fp_out.write(sync_word) - fp_out.write(n_bits_bs) - fp_out.write(fp.read(n_bits * 2)) - fr_cnt += 1 - extracted_frames_cnt += 1 - length -= 1 + while length != 0 and not eof: + (sync_word, n_bits, data, eof) = get_next_frame(fp) + if not eof: + write_frame(fp_out, sync_word, n_bits, data) + fr_cnt += 1 + extracted_frames_cnt += 1 + length -= 1 # count remaining frames, if any while not eof: - sync_word = fp.read(2) - if sync_word == b"": - eof = True - break - if sync_word not in SYNC_WORDS: - raise ValueError("Bad Sync word!") - n_bits_bs = fp.read(2) - n_bits = struct.unpack("h", n_bits_bs)[0] - fp.read(n_bits * 2) - fr_cnt += 1 + (sync_word, n_bits, data, eof) = get_next_frame(fp) + if not eof: + fr_cnt += 1 return (fr_cnt, extracted_frames_cnt) @@ -130,19 +125,25 @@ if __name__ == "__main__": if not os.path.exists(os.path.realpath(my_args.bs_in)): print(f"\nInput file {my_args.bs_in} does not exist!") sys.exit(-1) - if not os.path.exists(os.path.realpath(my_args.bs_out)): - print(f"\nOutput file {my_args.bs_out} does not exist, creating it!") - else: - print(f"\nOutput file {my_args.bs_out} does exist, overwriting it!") + if os.path.exists(os.path.realpath(my_args.bs_out)): + print(f"\nOutput file {my_args.bs_out} exists, overwriting it!") with open(my_args.bs_in, "rb") as fp_in: with open(my_args.bs_out, "wb") as fp_out: fr_cnt, extracted_frames_cnt = cut_from_start( - fp_in, fp_out, start_frame=my_args.frame, length=my_args.length, start_with_sid=my_args.sid + fp_in, + fp_out, + start_frame=my_args.frame, + length=my_args.length, + start_with_sid=my_args.sid, ) if extracted_frames_cnt == 0: print(f"\nExtracted 0 frames!") sys.exit(-1) - if (my_args.length != -1 and extracted_frames_cnt != my_args.length): - print(f"Warning! Requested {my_args.length}, only {extracted_frames_cnt} obtained!") - print(f"Extracted {extracted_frames_cnt} out of {fr_cnt} frames from {my_args.bs_in}") + if my_args.length != -1 and extracted_frames_cnt != my_args.length: + print( + f"Warning! Requested {my_args.length}, only {extracted_frames_cnt} obtained!" + ) + print( + f"Extracted {extracted_frames_cnt} out of {fr_cnt} frames from {my_args.bs_in}" + ) -- GitLab From 6937002f4d26e39a329e5e78a1217e1ecc814a05 Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Mon, 30 Sep 2024 08:00:58 +0200 Subject: [PATCH 4/4] Fix frame count in cut_bs.py --- scripts/cut_bs.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/cut_bs.py b/scripts/cut_bs.py index fe855846af..e54e99fdfe 100755 --- a/scripts/cut_bs.py +++ b/scripts/cut_bs.py @@ -75,10 +75,11 @@ def cut_from_start(fp, fp_out, start_frame=0, length=-1, start_with_sid=False): while not found and not eof: (sync_word, n_bits, data, eof) = get_next_frame(fp) if not eof: - fr_cnt += 1 if n_bits in SID_BITS: found = True fp.seek(-(4 + n_bits * 2), 1) # Found SID, rewind + else: + fr_cnt += 1 # transfer frames while length != 0 and not eof: (sync_word, n_bits, data, eof) = get_next_frame(fp) -- GitLab