- while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
- ++buf;
- --length;
- if (length < 4) {
- if (pkt->nb_nals > 0) {
- // No more start codes: we discarded some irrelevant
- // bytes at the end of the packet.
- return 0;
- } else {
- av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
- return AVERROR_INVALIDDATA;
- }
- } else if (buf >= (next_avc - 3))
- break;
+ buf_index = find_next_start_code(bc.buffer, buf + next_avc);
+
+ bytestream2_skip(&bc, buf_index);
+
+ if (!bytestream2_get_bytes_left(&bc)) {
+ if (pkt->nb_nals > 0) {
+ // No more start codes: we discarded some irrelevant
+ // bytes at the end of the packet.
+ return 0;
+ } else {
+ av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
+ return AVERROR_INVALIDDATA;
+ }