FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/movenccenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 5 205 2.4%
Functions: 1 18 5.6%
Branches: 0 68 0.0%

Line Branch Exec Source
1 /*
2 * MOV CENC (Common Encryption) writer
3 * Copyright (c) 2015 Eran Kornblau <erankor at gmail dot com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 #include "movenccenc.h"
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/mem.h"
24 #include "avio_internal.h"
25 #include "movenc.h"
26 #include "avc.h"
27 #include "nal.h"
28
29 static int auxiliary_info_alloc_size(MOVMuxCencContext* ctx, int size)
30 {
31 size_t new_alloc_size;
32
33 if (ctx->auxiliary_info_size + size > ctx->auxiliary_info_alloc_size) {
34 new_alloc_size = FFMAX(ctx->auxiliary_info_size + size, ctx->auxiliary_info_alloc_size * 2);
35 if (av_reallocp(&ctx->auxiliary_info, new_alloc_size)) {
36 return AVERROR(ENOMEM);
37 }
38
39 ctx->auxiliary_info_alloc_size = new_alloc_size;
40 }
41
42 return 0;
43 }
44
45 static int auxiliary_info_write(MOVMuxCencContext* ctx,
46 const uint8_t *buf_in, int size)
47 {
48 int ret;
49
50 ret = auxiliary_info_alloc_size(ctx, size);
51 if (ret) {
52 return ret;
53 }
54 memcpy(ctx->auxiliary_info + ctx->auxiliary_info_size, buf_in, size);
55 ctx->auxiliary_info_size += size;
56
57 return 0;
58 }
59
60 static int auxiliary_info_add_subsample(MOVMuxCencContext* ctx,
61 uint16_t clear_bytes, uint32_t encrypted_bytes)
62 {
63 uint8_t* p;
64 int ret;
65
66 if (!ctx->use_subsamples) {
67 return 0;
68 }
69
70 ret = auxiliary_info_alloc_size(ctx, 6);
71 if (ret) {
72 return ret;
73 }
74
75 p = ctx->auxiliary_info + ctx->auxiliary_info_size;
76
77 AV_WB16(p, clear_bytes);
78 p += sizeof(uint16_t);
79
80 AV_WB32(p, encrypted_bytes);
81
82 ctx->auxiliary_info_size += 6;
83 ctx->subsample_count++;
84
85 return 0;
86 }
87
88 /**
89 * Encrypt the input buffer and write using avio_write
90 */
91 static void mov_cenc_write_encrypted(MOVMuxCencContext* ctx, AVIOContext *pb,
92 const uint8_t *buf_in, int size)
93 {
94 uint8_t chunk[4096];
95 const uint8_t* cur_pos = buf_in;
96 int size_left = size;
97 int cur_size;
98
99 while (size_left > 0) {
100 cur_size = FFMIN(size_left, sizeof(chunk));
101 av_aes_ctr_crypt(ctx->aes_ctr, chunk, cur_pos, cur_size);
102 avio_write(pb, chunk, cur_size);
103 cur_pos += cur_size;
104 size_left -= cur_size;
105 }
106 }
107
108 /**
109 * Start writing a packet
110 */
111 static int mov_cenc_start_packet(MOVMuxCencContext* ctx)
112 {
113 int ret;
114
115 /* write the iv */
116 ret = auxiliary_info_write(ctx, av_aes_ctr_get_iv(ctx->aes_ctr), AES_CTR_IV_SIZE);
117 if (ret) {
118 return ret;
119 }
120
121 if (!ctx->use_subsamples) {
122 return 0;
123 }
124
125 /* write a zero subsample count */
126 ctx->auxiliary_info_subsample_start = ctx->auxiliary_info_size;
127 ctx->subsample_count = 0;
128 ret = auxiliary_info_write(ctx, (uint8_t*)&ctx->subsample_count, sizeof(ctx->subsample_count));
129 if (ret) {
130 return ret;
131 }
132
133 return 0;
134 }
135
136 /**
137 * Finalize a packet
138 */
139 static int mov_cenc_end_packet(MOVMuxCencContext* ctx)
140 {
141 size_t new_alloc_size;
142
143 av_aes_ctr_increment_iv(ctx->aes_ctr);
144
145 if (!ctx->use_subsamples) {
146 ctx->auxiliary_info_entries++;
147 return 0;
148 }
149
150 /* add the auxiliary info entry size*/
151 if (ctx->auxiliary_info_entries >= ctx->auxiliary_info_sizes_alloc_size) {
152 new_alloc_size = ctx->auxiliary_info_entries * 2 + 1;
153 if (av_reallocp(&ctx->auxiliary_info_sizes, new_alloc_size)) {
154 return AVERROR(ENOMEM);
155 }
156
157 ctx->auxiliary_info_sizes_alloc_size = new_alloc_size;
158 }
159 ctx->auxiliary_info_sizes[ctx->auxiliary_info_entries] =
160 AES_CTR_IV_SIZE + ctx->auxiliary_info_size - ctx->auxiliary_info_subsample_start;
161 ctx->auxiliary_info_entries++;
162
163 /* update the subsample count*/
164 AV_WB16(ctx->auxiliary_info + ctx->auxiliary_info_subsample_start, ctx->subsample_count);
165
166 return 0;
167 }
168
169 int ff_mov_cenc_write_packet(MOVMuxCencContext* ctx, AVIOContext *pb,
170 const uint8_t *buf_in, int size)
171 {
172 int ret;
173
174 ret = mov_cenc_start_packet(ctx);
175 if (ret) {
176 return ret;
177 }
178
179 ret = auxiliary_info_add_subsample(ctx, 0, size);
180 if (ret) {
181 return ret;
182 }
183
184 mov_cenc_write_encrypted(ctx, pb, buf_in, size);
185
186 ret = mov_cenc_end_packet(ctx);
187 if (ret) {
188 return ret;
189 }
190
191 return 0;
192 }
193
194 int ff_mov_cenc_avc_parse_nal_units(MOVMuxCencContext* ctx, AVIOContext *pb,
195 const uint8_t *buf_in, int size)
196 {
197 const uint8_t *p = buf_in;
198 const uint8_t *end = p + size;
199 const uint8_t *nal_start, *nal_end;
200 int ret;
201
202 ret = mov_cenc_start_packet(ctx);
203 if (ret) {
204 return ret;
205 }
206
207 size = 0;
208 nal_start = ff_nal_find_startcode(p, end);
209 for (;;) {
210 while (nal_start < end && !*(nal_start++));
211 if (nal_start == end)
212 break;
213
214 nal_end = ff_nal_find_startcode(nal_start, end);
215
216 avio_wb32(pb, nal_end - nal_start);
217 avio_w8(pb, *nal_start);
218 mov_cenc_write_encrypted(ctx, pb, nal_start + 1, nal_end - nal_start - 1);
219
220 auxiliary_info_add_subsample(ctx, 5, nal_end - nal_start - 1);
221
222 size += 4 + nal_end - nal_start;
223 nal_start = nal_end;
224 }
225
226 ret = mov_cenc_end_packet(ctx);
227 if (ret) {
228 return ret;
229 }
230
231 return size;
232 }
233
234 int ff_mov_cenc_avc_write_nal_units(AVFormatContext *s, MOVMuxCencContext* ctx,
235 int nal_length_size, AVIOContext *pb, const uint8_t *buf_in, int size)
236 {
237 int nalsize;
238 int ret;
239 int j;
240
241 ret = mov_cenc_start_packet(ctx);
242 if (ret) {
243 return ret;
244 }
245
246 while (size > 0) {
247 /* parse the nal size */
248 if (size < nal_length_size + 1) {
249 av_log(s, AV_LOG_ERROR, "CENC-AVC: remaining size %d smaller than nal length+type %d\n",
250 size, nal_length_size + 1);
251 return -1;
252 }
253
254 avio_write(pb, buf_in, nal_length_size + 1);
255
256 nalsize = 0;
257 for (j = 0; j < nal_length_size; j++) {
258 nalsize = (nalsize << 8) | *buf_in++;
259 }
260 size -= nal_length_size;
261
262 /* encrypt the nal body */
263 if (nalsize <= 0 || nalsize > size) {
264 av_log(s, AV_LOG_ERROR, "CENC-AVC: nal size %d remaining %d\n", nalsize, size);
265 return -1;
266 }
267
268 mov_cenc_write_encrypted(ctx, pb, buf_in + 1, nalsize - 1);
269 buf_in += nalsize;
270 size -= nalsize;
271
272 auxiliary_info_add_subsample(ctx, nal_length_size + 1, nalsize - 1);
273 }
274
275 ret = mov_cenc_end_packet(ctx);
276 if (ret) {
277 return ret;
278 }
279
280 return 0;
281 }
282
283 /* TODO: reuse this function from movenc.c */
284 static int64_t update_size(AVIOContext *pb, int64_t pos)
285 {
286 int64_t curpos = avio_tell(pb);
287 avio_seek(pb, pos, SEEK_SET);
288 avio_wb32(pb, curpos - pos); /* rewrite size */
289 avio_seek(pb, curpos, SEEK_SET);
290
291 return curpos - pos;
292 }
293
294 static int mov_cenc_write_senc_tag(MOVMuxCencContext* ctx, AVIOContext *pb,
295 int64_t* auxiliary_info_offset)
296 {
297 int64_t pos = avio_tell(pb);
298
299 avio_wb32(pb, 0); /* size */
300 ffio_wfourcc(pb, "senc");
301 avio_wb32(pb, ctx->use_subsamples ? 0x02 : 0); /* version & flags */
302 avio_wb32(pb, ctx->auxiliary_info_entries); /* entry count */
303 *auxiliary_info_offset = avio_tell(pb);
304 avio_write(pb, ctx->auxiliary_info, ctx->auxiliary_info_size);
305 return update_size(pb, pos);
306 }
307
308 static int mov_cenc_write_saio_tag(AVIOContext *pb, int64_t auxiliary_info_offset)
309 {
310 int64_t pos = avio_tell(pb);
311 uint8_t version;
312
313 avio_wb32(pb, 0); /* size */
314 ffio_wfourcc(pb, "saio");
315 version = auxiliary_info_offset > 0xffffffff ? 1 : 0;
316 avio_w8(pb, version);
317 avio_wb24(pb, 0); /* flags */
318 avio_wb32(pb, 1); /* entry count */
319 if (version) {
320 avio_wb64(pb, auxiliary_info_offset);
321 } else {
322 avio_wb32(pb, auxiliary_info_offset);
323 }
324 return update_size(pb, pos);
325 }
326
327 static int mov_cenc_write_saiz_tag(MOVMuxCencContext* ctx, AVIOContext *pb)
328 {
329 int64_t pos = avio_tell(pb);
330 avio_wb32(pb, 0); /* size */
331 ffio_wfourcc(pb, "saiz");
332 avio_wb32(pb, 0); /* version & flags */
333 avio_w8(pb, ctx->use_subsamples ? 0 : AES_CTR_IV_SIZE); /* default size*/
334 avio_wb32(pb, ctx->auxiliary_info_entries); /* entry count */
335 if (ctx->use_subsamples) {
336 avio_write(pb, ctx->auxiliary_info_sizes, ctx->auxiliary_info_entries);
337 }
338 return update_size(pb, pos);
339 }
340
341 void ff_mov_cenc_write_stbl_atoms(MOVMuxCencContext* ctx, AVIOContext *pb)
342 {
343 int64_t auxiliary_info_offset;
344
345 mov_cenc_write_senc_tag(ctx, pb, &auxiliary_info_offset);
346 mov_cenc_write_saio_tag(pb, auxiliary_info_offset);
347 mov_cenc_write_saiz_tag(ctx, pb);
348 }
349
350 static int mov_cenc_write_schi_tag(AVIOContext *pb, uint8_t* kid)
351 {
352 int64_t pos = avio_tell(pb);
353 avio_wb32(pb, 0); /* size */
354 ffio_wfourcc(pb, "schi");
355
356 avio_wb32(pb, 32); /* size */
357 ffio_wfourcc(pb, "tenc");
358 avio_wb32(pb, 0); /* version & flags */
359 avio_wb24(pb, 1); /* is encrypted */
360 avio_w8(pb, AES_CTR_IV_SIZE); /* iv size */
361 avio_write(pb, kid, CENC_KID_SIZE);
362
363 return update_size(pb, pos);
364 }
365
366 int ff_mov_cenc_write_sinf_tag(MOVTrack* track, AVIOContext *pb, uint8_t* kid)
367 {
368 int64_t pos = avio_tell(pb);
369 avio_wb32(pb, 0); /* size */
370 ffio_wfourcc(pb, "sinf");
371
372 /* frma */
373 avio_wb32(pb, 12); /* size */
374 ffio_wfourcc(pb, "frma");
375 avio_wl32(pb, track->tag);
376
377 /* schm */
378 avio_wb32(pb, 20); /* size */
379 ffio_wfourcc(pb, "schm");
380 avio_wb32(pb, 0); /* version & flags */
381 ffio_wfourcc(pb, "cenc"); /* scheme type*/
382 avio_wb32(pb, 0x10000); /* scheme version */
383
384 /* schi */
385 mov_cenc_write_schi_tag(pb, kid);
386
387 return update_size(pb, pos);
388 }
389
390 int ff_mov_cenc_init(MOVMuxCencContext* ctx, uint8_t* encryption_key,
391 int use_subsamples, int bitexact)
392 {
393 int ret;
394
395 ctx->aes_ctr = av_aes_ctr_alloc();
396 if (!ctx->aes_ctr) {
397 return AVERROR(ENOMEM);
398 }
399
400 ret = av_aes_ctr_init(ctx->aes_ctr, encryption_key);
401 if (ret != 0) {
402 return ret;
403 }
404
405 if (!bitexact) {
406 av_aes_ctr_set_random_iv(ctx->aes_ctr);
407 }
408
409 ctx->use_subsamples = use_subsamples;
410
411 return 0;
412 }
413
414 276 void ff_mov_cenc_free(MOVMuxCencContext* ctx)
415 {
416 276 av_aes_ctr_free(ctx->aes_ctr);
417 276 av_freep(&ctx->auxiliary_info);
418 276 av_freep(&ctx->auxiliary_info_sizes);
419 276 }
420