start_code_size depends on whether PS comes from out-of-band or in-band. Make the code more readable.
Unified · Split
| @@ -43,10 +43,26 @@ typedef struct H264BSFContext { · 42 unmodified lines | ||
| 43 | 43 | int extradata_parsed; |
| 44 | 44 | } H264BSFContext; |
| 45 | 45 | |
| 46 | +enum PsSource { | |
| 47 | + PS_OUT_OF_BAND = -1, | |
| 48 | + PS_NONE = 0, | |
| 49 | + PS_IN_BAND = 1, | |
| 50 | +}; | |
| 51 | + | |
| 46 | 52 | static void count_or_copy(uint8_t **out, uint64_t *out_size, |
| 47 | − const uint8_t *in, int in_size, int ps, int copy) | |
| 53 | + const uint8_t *in, int in_size, enum PsSource ps, int copy) | |
| 48 | 54 | { |
| 49 | − uint8_t start_code_size = ps < 0 ? 0 : *out_size == 0 || ps ? 4 : 3; | |
| 55 | + uint8_t start_code_size; | |
| 56 | + | |
| 57 | + if (ps == PS_OUT_OF_BAND) | |
| 58 | + /* start code already present in out-of-band ps data, so don't need to | |
| 59 | + * add it manually again | |
| 60 | + */ | |
| 61 | + start_code_size = 0; | |
| 62 | + else if (ps == PS_IN_BAND || *out_size == 0) | |
| 63 | + start_code_size = 4; | |
| 64 | + else | |
| 65 | + start_code_size = 3; | |
| 50 | 66 | |
| 51 | 67 | if (copy) { |
| 52 | 68 | memcpy(*out + start_code_size, in, in_size); |
| @@ -202,6 +218,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 149 unmodified lines | ||
| 202 | 218 | |
| 203 | 219 | do { |
| 204 | 220 | uint32_t nal_size = 0; |
| 221 | + enum PsSource ps; | |
| 205 | 222 | |
| 206 | 223 | /* possible overread ok due to padding */ |
| 207 | 224 | for (int i = 0; i < s->length_size; i++) |
| @@ -230,7 +247,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 22 unmodified lines | ||
| 230 | 247 | if (!s->sps_size) { |
| 231 | 248 | LOG_ONCE(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n"); |
| 232 | 249 | } else { |
| 233 | − count_or_copy(&out, &out_size, s->sps, s->sps_size, -1, j); | |
| 250 | + count_or_copy(&out, &out_size, s->sps, s->sps_size, PS_OUT_OF_BAND, j); | |
| 234 | 251 | sps_seen = 1; |
| 235 | 252 | } |
| 236 | 253 | } |
| @@ -246,19 +263,22 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 9 unmodified lines | ||
| 246 | 263 | if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) { |
| 247 | 264 | if (ctx->par_out->extradata) |
| 248 | 265 | count_or_copy(&out, &out_size, ctx->par_out->extradata, |
| 249 | − ctx->par_out->extradata_size, -1, j); | |
| 266 | + ctx->par_out->extradata_size, PS_OUT_OF_BAND, j); | |
| 250 | 267 | new_idr = 0; |
| 251 | 268 | /* if only SPS has been seen, also insert PPS */ |
| 252 | 269 | } else if (new_idr && unit_type == H264_NAL_IDR_SLICE && sps_seen && !pps_seen) { |
| 253 | 270 | if (!s->pps_size) { |
| 254 | 271 | LOG_ONCE(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n"); |
| 255 | 272 | } else { |
| 256 | − count_or_copy(&out, &out_size, s->pps, s->pps_size, -1, j); | |
| 273 | + count_or_copy(&out, &out_size, s->pps, s->pps_size, PS_OUT_OF_BAND, j); | |
| 257 | 274 | } |
| 258 | 275 | } |
| 259 | 276 | |
| 260 | − count_or_copy(&out, &out_size, buf, nal_size, | |
| 261 | − unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS, j); | |
| 277 | + if (unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS) | |
| 278 | + ps = PS_IN_BAND; | |
| 279 | + else | |
| 280 | + ps = PS_NONE; | |
| 281 | + count_or_copy(&out, &out_size, buf, nal_size, ps, j); | |
| 262 | 282 | if (!new_idr && unit_type == H264_NAL_SLICE) { |
| 263 | 283 | new_idr = 1; |
| 264 | 284 | sps_seen = 0; |