1 /*
2 * Ut Video encoder
3 * Copyright (c) 2012 Jan Ekström
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
22 /**
23 * @file
24 * Ut Video encoder
25 */
26
32
42
47
53
58
64
65 /* Compare huffman tree nodes */
67 {
69 return (aa->
len - bb->len)*256 + aa->
sym - bb->sym;
70 }
71
72 /* Compare huffentry symbols */
74 {
76 return aa->
sym - bb->sym;
77 }
78
80 {
83
85 for (
i = 0;
i < 4;
i++)
87
88 return 0;
89 }
90
92 {
94 int i, subsampled_height;
95 uint32_t original_format;
96
97 c->frame_info_size = 4;
99
105 break;
111 break;
115 "4:2:0 video requires even width and height.\n");
117 }
121 else
124 break;
126 if (avctx->
width & 1) {
128 "4:2:2 video requires even width.\n");
130 }
134 else
137 break;
142 else
145 break;
146 default:
148 }
149
152
156 }
157
158 /*
159 * Check the asked slice count for obviously invalid
160 * values (> 256 or negative).
161 */
164 "Slice count %d is not supported in Ut Video (theoretical range is 0-256).\n",
167 }
168
169 /* Check that the slice count is not larger than the subsampled height */
171 if (avctx->
slices > subsampled_height) {
173 "Slice count %d is larger than the subsampling-applied height %d.\n",
174 avctx->
slices, subsampled_height);
176 }
177
178 /* extradata size is 4 * 32 bits */
180
183
187 }
188
189 for (
i = 0;
i <
c->planes;
i++) {
192 if (!
c->slice_buffer[
i]) {
195 }
196 }
197
198 /*
199 * Set the version of the encoder.
200 * Last byte is "implementation ID", which is
201 * obtained from the creator of the format.
202 * Libavcodec has been assigned with the ID 0xF0.
203 */
205
206 /*
207 * Set the "original format"
208 * Not used for anything during decoding.
209 */
211
212 /* Write 4 as the 'frame info size' */
214
215 /*
216 * Set how many slices are going to be used.
217 * By default uses multiple slices depending on the subsampled height.
218 * This enables multithreading in the official decoder.
219 */
221 c->slices = subsampled_height / 120;
222
225 else if (
c->slices > 256)
227 } else {
229 }
230
231 /* Set compression mode */
233
234 /*
235 * Set the encoding flags:
236 * - Slice count minus 1
237 * - Interlaced encoding mode flag, set to zero for now.
238 * - Compression mode (none/huff)
239 * And write the flags.
240 */
241 c->flags = (
c->slices - 1
U) << 24;
242 c->flags |= 0 << 11;
// bit field to signal interlaced encoding mode
243 c->flags |=
c->compression;
244
246
247 return 0;
248 }
249
253 {
255 int k = 2 * dst_stride;
256 const uint8_t *sg =
src[0];
257 const uint8_t *sb =
src[1];
258 const uint8_t *sr =
src[2];
259 const uint8_t *sa =
src[3];
261
262 for (j = 0; j <
height; j++) {
268 dst[1][k] = sb[
i] -
g;
269 dst[2][k] = sr[
i] -
g;
270 k++;
271 }
272 } else {
277 dst[1][k] = sb[
i] -
g;
278 dst[2][k] = sr[
i] -
g;
280 k++;
281 }
283 }
284 k += dst_stride -
width;
288 }
289 }
290
291 #undef A
292 #undef B
293
294 /* Write data to a plane with median prediction */
297 {
300 uint8_t prev;
301
302 /* First line uses left neighbour prediction */
303 prev = 0x80; /* Set the initial value */
307 }
308
310 return;
311
313
314 /*
315 * Second line uses top prediction for the first sample,
316 * and median for the rest.
317 */
319
320 /* Rest of the coded part uses median prediction */
321 for (j = 1; j <
height; j++) {
325 }
326 }
327
328 /* Count the usage of values in a plane */
330 int height, uint64_t *counts)
331 {
333
334 for (j = 0; j <
height; j++) {
337 }
339 }
340 }
341
342 /* Calculate the actual huffman codes from the code lengths */
344 {
347
349
350 last = 255;
351 while (he[last].
len == 255 && last)
352 last--;
353
355 for (
i = last;
i >= 0;
i--) {
358 }
359
361 }
362
363 /* Write huffman bit codes to a memory block */
366 {
369 int count;
370
372
373 /* Write the codes */
374 for (j = 0; j <
height; j++) {
377
379 }
380
381 /* Pad output to a 32-bit boundary */
383
384 if (count)
386
387 /* Flush the rest with zeroes */
389
390 /* Return the amount of bytes written */
392 }
393
395 uint8_t *
dst, ptrdiff_t
stride,
int plane_no,
397 {
399 uint8_t lengths[256];
400 uint64_t counts[256] = { 0 };
401
403
404 uint32_t
offset = 0, slice_len = 0;
406 int i, sstart, send = 0;
407 int symbol;
409
410 /* Do prediction / make planes */
411 switch (
c->frame_pred) {
413 for (
i = 0;
i <
c->slices;
i++) {
414 sstart = send;
415 send =
height * (
i + 1) /
c->slices & cmask;
418 width, send - sstart);
419 }
420 break;
422 for (
i = 0;
i <
c->slices;
i++) {
423 sstart = send;
424 send =
height * (
i + 1) /
c->slices & cmask;
426 }
427 break;
429 for (
i = 0;
i <
c->slices;
i++) {
430 sstart = send;
431 send =
height * (
i + 1) /
c->slices & cmask;
434 }
435 break;
436 default:
440 }
441
442 /* Count the usage of values */
444
445 /* Check for a special case where only one symbol was used */
446 for (symbol = 0; symbol < 256; symbol++) {
447 /* If non-zero count is found, see if it matches width * height */
448 if (counts[symbol]) {
449 /* Special case if only one symbol was used */
451 /*
452 * Write a zero for the single symbol
453 * used in the plane, else 0xFF.
454 */
455 for (
i = 0;
i < 256;
i++) {
457 bytestream2_put_byte(pb, 0);
458 else
459 bytestream2_put_byte(pb, 0xFF);
460 }
461
462 /* Write zeroes for lengths */
463 for (
i = 0;
i <
c->slices;
i++)
464 bytestream2_put_le32(pb, 0);
465
466 /* And that's all for that plane folks */
467 return 0;
468 }
469 break;
470 }
471 }
472
473 /* Calculate huffman lengths */
476
477 /*
478 * Write the plane's header into the output packet:
479 * - huffman code lengths (256 bytes)
480 * - slice end offsets (gotten from the slice lengths)
481 */
482 for (
i = 0;
i < 256;
i++) {
483 bytestream2_put_byte(pb, lengths[
i]);
484
485 he[
i].
len = lengths[
i];
487 }
488
489 /* Calculate the huffman codes themselves */
491
492 send = 0;
493 for (
i = 0;
i <
c->slices;
i++) {
494 sstart = send;
495 send =
height * (
i + 1) /
c->slices & cmask;
496
497 /*
498 * Write the huffman codes to a buffer,
499 * get the offset in bytes.
500 */
503 send - sstart, he);
504
505 slice_len =
offset - slice_len;
506
507 /* Byteswap the written huffman codes */
508 c->bdsp.bswap_buf((uint32_t *)
c->slice_bits,
509 (uint32_t *)
c->slice_bits,
510 slice_len >> 2);
511
512 /* Write the offset to the stream */
513 bytestream2_put_le32(pb,
offset);
514
515 /* Seek to the data part of the packet */
517 offset - slice_len, SEEK_CUR);
518
519 /* Write the slices' data into the output packet */
521
522 /* Seek back to the slice offsets */
524 SEEK_CUR);
525
527 }
528
529 /* And at the end seek to the end of written slice(s) */
531
532 return 0;
533 }
534
536 const AVFrame *pic,
int *got_packet)
537 {
540
542
544
547
548 /* Allocate a new packet if needed, and set it to the pointer dst */
551
554
556
558
560
561 if (!
c->slice_bits) {
564 }
565
566 /* In case of RGB, mangle the planes to Ut Video's format */
570
571 /* Deal with the planes */
575 for (
i = 0;
i <
c->planes;
i++) {
577 c->slice_buffer[
i],
c->slice_stride,
i,
579
583 }
584 }
585 break;
587 for (
i = 0;
i <
c->planes;
i++) {
590
594 }
595 }
596 break;
598 for (
i = 0;
i <
c->planes;
i++) {
601
605 }
606 }
607 break;
609 for (
i = 0;
i <
c->planes;
i++) {
612 &pb);
613
617 }
618 }
619 break;
620 default:
624 }
625
626 /*
627 * Write frame information (LE 32-bit unsigned)
628 * into the output packet.
629 * Contains the prediction method.
630 */
633
635
636 /* Packet should be done */
637 *got_packet = 1;
638
639 return 0;
640 }
641
642 #define OFFSET(x) offsetof(UtvideoContext, x)
643 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
649
651 };
652
658 };
659
676 };