History

avcodec/h264_mp4toannexb_bsf: refactor start_code_size handling

start_code_size depends on whether PS comes from out-of-band or in-band. Make the code more readable.

Zhao Zhili committed 3 years ago

Commit 91cbae21 parent fb520701 file · +27 −7Browse files

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/h264_mp4toannexb_bsf.c+27−7
@@ -43,10 +43,26 @@ typedef struct H264BSFContext { · 42 unmodified lines
4343 int extradata_parsed;
4444 } H264BSFContext;
4545
46+enum PsSource {
47+ PS_OUT_OF_BAND = -1,
48+ PS_NONE = 0,
49+ PS_IN_BAND = 1,
50+};
51+
4652 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)
4854 {
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;
5066
5167 if (copy) {
5268 memcpy(*out + start_code_size, in, in_size);
@@ -202,6 +218,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 149 unmodified lines
202218
203219 do {
204220 uint32_t nal_size = 0;
221+ enum PsSource ps;
205222
206223 /* possible overread ok due to padding */
207224 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
230247 if (!s->sps_size) {
231248 LOG_ONCE(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n");
232249 } 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);
234251 sps_seen = 1;
235252 }
236253 }
@@ -246,19 +263,22 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 9 unmodified lines
246263 if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) {
247264 if (ctx->par_out->extradata)
248265 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);
250267 new_idr = 0;
251268 /* if only SPS has been seen, also insert PPS */
252269 } else if (new_idr && unit_type == H264_NAL_IDR_SLICE && sps_seen && !pps_seen) {
253270 if (!s->pps_size) {
254271 LOG_ONCE(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n");
255272 } 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);
257274 }
258275 }
259276
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);
262282 if (!new_idr && unit_type == H264_NAL_SLICE) {
263283 new_idr = 1;
264284 sps_seen = 0;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t