start_code_size depends on whether PS comes from out-of-band or in-band. Make the code more readable.
Showing the changes to libavcodec (1 of 1 file). Show all
Unified · Split
| @@ -43,10 +43,26 @@ typedef struct H264BSFContext { · 42 unmodified lines | |||
| 43 | int extradata_parsed; | 43 | int extradata_parsed; |
| 44 | } H264BSFContext; | 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 | static void count_or_copy(uint8_t **out, uint64_t *out_size, | 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 | if (copy) { | 67 | if (copy) { |
| 52 | memcpy(*out + start_code_size, in, in_size); | 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 | do { | 219 | do { |
| 204 | uint32_t nal_size = 0; | 220 | uint32_t nal_size = 0; |
| 221 | enum PsSource ps; | ||
| 205 | 222 | ||
| 206 | /* possible overread ok due to padding */ | 223 | /* possible overread ok due to padding */ |
| 207 | for (int i = 0; i < s->length_size; i++) | 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 | if (!s->sps_size) { | 247 | if (!s->sps_size) { |
| 231 | LOG_ONCE(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n"); | 248 | LOG_ONCE(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n"); |
| 232 | } else { | 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 | sps_seen = 1; | 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 | if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) { | 263 | if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) { |
| 247 | if (ctx->par_out->extradata) | 264 | if (ctx->par_out->extradata) |
| 248 | count_or_copy(&out, &out_size, ctx->par_out->extradata, | 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 | new_idr = 0; | 267 | new_idr = 0; |
| 251 | /* if only SPS has been seen, also insert PPS */ | 268 | /* if only SPS has been seen, also insert PPS */ |
| 252 | } else if (new_idr && unit_type == H264_NAL_IDR_SLICE && sps_seen && !pps_seen) { | 269 | } else if (new_idr && unit_type == H264_NAL_IDR_SLICE && sps_seen && !pps_seen) { |
| 253 | if (!s->pps_size) { | 270 | if (!s->pps_size) { |
| 254 | LOG_ONCE(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n"); | 271 | LOG_ONCE(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n"); |
| 255 | } else { | 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, | 277 | if (unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS) |
| 261 | unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS, j); | 278 | ps = PS_IN_BAND; |
| 279 | else | ||
| 280 | ps = PS_NONE; | ||
| 281 | count_or_copy(&out, &out_size, buf, nal_size, ps, j); | ||
| 262 | if (!new_idr && unit_type == H264_NAL_SLICE) { | 282 | if (!new_idr && unit_type == H264_NAL_SLICE) { |
| 263 | new_idr = 1; | 283 | new_idr = 1; |
| 264 | sps_seen = 0; | 284 | sps_seen = 0; |