|
| 1 | +#include "mp4-writer.h" |
| 2 | +#include "mov-format.h" |
| 3 | +#include "webm-vpx.h" |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | +#include <assert.h> |
| 8 | + |
| 9 | +extern "C" const struct mov_buffer_t* mov_file_buffer(void); |
| 10 | + |
| 11 | +static uint8_t s_buffer[2 * 1024 * 1024]; |
| 12 | +static uint8_t s_extra_data[64 * 1024]; |
| 13 | + |
| 14 | +struct mov_vpx_test_t |
| 15 | +{ |
| 16 | + mp4_writer_t* mov; |
| 17 | + struct webm_vpx_t vpx; |
| 18 | + |
| 19 | + int track; |
| 20 | + int width; |
| 21 | + int height; |
| 22 | + uint32_t pts, dts; |
| 23 | + uint8_t* ptr; |
| 24 | + int bytes; |
| 25 | + |
| 26 | + int vcl; |
| 27 | + int keyframe; |
| 28 | +}; |
| 29 | + |
| 30 | +static uint8_t* file_read(const char* file, long* size) |
| 31 | +{ |
| 32 | + FILE* fp = fopen(file, "rb"); |
| 33 | + if (fp) |
| 34 | + { |
| 35 | + fseek(fp, 0, SEEK_END); |
| 36 | + *size = ftell(fp); |
| 37 | + fseek(fp, 0, SEEK_SET); |
| 38 | + |
| 39 | + uint8_t* ptr = (uint8_t*)malloc(*size); |
| 40 | + fread(ptr, 1, *size, fp); |
| 41 | + fclose(fp); |
| 42 | + |
| 43 | + return ptr; |
| 44 | + } |
| 45 | + |
| 46 | + return NULL; |
| 47 | +} |
| 48 | + |
| 49 | +static int vpx_write(struct mov_vpx_test_t* ctx, const void* data, int bytes) |
| 50 | +{ |
| 51 | + if (ctx->track < 0) |
| 52 | + { |
| 53 | + int r = webm_vpx_codec_configuration_record_from_vp9(&ctx->vpx, &ctx->width, &ctx->height, data, bytes); |
| 54 | + if (ctx->width < 1 || ctx->height < 1) |
| 55 | + { |
| 56 | + //ctx->ptr = end; |
| 57 | + return -2; // waiting for sps/pps |
| 58 | + } |
| 59 | + |
| 60 | + int extra_data_size = webm_vpx_codec_configuration_record_save(&ctx->vpx, s_extra_data, sizeof(s_extra_data)); |
| 61 | + if (extra_data_size <= 0) |
| 62 | + { |
| 63 | + // invalid AVCC |
| 64 | + assert(0); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + // TODO: waiting for key frame ??? |
| 69 | + ctx->track = mp4_writer_add_video(ctx->mov, MOV_OBJECT_VP9, ctx->width, ctx->height, s_extra_data, extra_data_size); |
| 70 | + if (ctx->track < 0) |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + mp4_writer_write(ctx->mov, ctx->track, data, bytes, ctx->pts, ctx->pts, ctx->keyframe ? MOV_AV_FLAG_KEYFREAME : 0); |
| 75 | + ctx->pts += 40; |
| 76 | + ctx->dts += 40; |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | +static int vpx_handler(struct mov_vpx_test_t* ctx, const uint8_t* ptr, size_t len) |
| 81 | +{ |
| 82 | + while (len > 2) |
| 83 | + { |
| 84 | + size_t n = (((size_t)ptr[0]) << 8) | ptr[1]; |
| 85 | + ptr += 2; |
| 86 | + len -= 2; |
| 87 | + if (n > len) |
| 88 | + return -1; |
| 89 | + |
| 90 | + vpx_write(ctx, ptr, n); |
| 91 | + ptr += n; |
| 92 | + len -= n; |
| 93 | + } |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +// file format: |
| 98 | +// 2ByteLenght(MSB) + vp9 payload + 2ByteLenght(MSB) + vp9 payload + ... |
| 99 | +void mov_writer_vp9(const char* file, int width, int height, const char* mp4) |
| 100 | +{ |
| 101 | + struct mov_vpx_test_t ctx; |
| 102 | + memset(&ctx, 0, sizeof(ctx)); |
| 103 | + ctx.track = -1; |
| 104 | + ctx.width = width; |
| 105 | + ctx.height = height; |
| 106 | + ctx.ptr = s_buffer; |
| 107 | + |
| 108 | + long bytes = 0; |
| 109 | + uint8_t* ptr = file_read(file, &bytes); |
| 110 | + if (NULL == ptr) return; |
| 111 | + |
| 112 | + FILE* fp = fopen(mp4, "wb+"); |
| 113 | + ctx.mov = mp4_writer_create(0, mov_file_buffer(), fp, 0); |
| 114 | + vpx_handler(&ctx, ptr, bytes); |
| 115 | + mp4_writer_destroy(ctx.mov); |
| 116 | + |
| 117 | + fclose(fp); |
| 118 | + free(ptr); |
| 119 | +} |
0 commit comments