[ create a new paste ] login | about

Link: http://codepad.org/uh7LvKJx    [ raw code | output | fork ]

C, pasted on Jan 19:
// DumpTS hevc to NAL hevc tool
// Video source only
// by 293

#include <stdio.h>
#include <stdlib.h>

#define BUF_SIZE 1000000

int main(int argc,char *argv[])
{
    FILE *fp_src, *fp_dst;

    int i;
    long rest_size, input_file_size, total_nal = 0;
    unsigned int nal_size;
    unsigned char nal_id;
    unsigned char buffer[BUF_SIZE];
    unsigned char header[5] = { 0x00, 0x00, 0x00, 0x01, 0 };
    

    if(argc < 3){
        printf("dt2nal input output\r\n");
        return -1;
    }
    
    if((fp_src = fopen(argv[1], "rb")) == NULL ) {
            perror("input file");
            exit(EXIT_FAILURE);
    }

    fseek(fp_src, 0L, SEEK_END);

    input_file_size = ftell(fp_src);

    fseek(fp_src,  0L, SEEK_SET);

    if((fp_dst = fopen(argv[2], "wb")) == NULL ) {
            perror(NULL);
            exit(EXIT_FAILURE);
    }
    
    fseek(fp_dst,  0L, SEEK_SET);
    
    printf("dt2nal processing...\r\n0%%");

    while(!feof(fp_src) && (input_file_size > ftell(fp_src))) {
        if(ferror(fp_src)){
            perror(NULL);
            exit(EXIT_FAILURE);
        }

        if(ferror(fp_dst)){
            perror(NULL);
            exit(EXIT_FAILURE);
        }

        nal_size  = fgetc(fp_src) << 24;
        nal_size |= fgetc(fp_src) << 16;
        nal_size |= fgetc(fp_src) << 8;
        nal_size |= fgetc(fp_src);

        if ((nal_size > input_file_size) || (ftell(fp_dst) > ftell(fp_src))) {
            printf("\r\nInvalid file type\r\n");
            fclose(fp_src);
            fclose(fp_dst);
            return -1;
        }

        nal_id = fgetc(fp_src);

        if (nal_id & 0x80) {
            printf("\r\nInvalid NAL:0x%02X offset:%ld\r\n", nal_id, ftell(fp_src) - 1);
            fclose(fp_src);
            fclose(fp_dst);
            return -1;
        }

        header[4] = nal_id;

        fwrite(header, 1, 5, fp_dst);

        rest_size = nal_size - 1;

        if ((ftell(fp_src) + rest_size) > input_file_size) {
            rest_size = input_file_size - ftell(fp_src);
        }

        while (rest_size > BUF_SIZE) {
            fread(buffer, 1, BUF_SIZE, fp_src);
            fwrite(buffer, 1, BUF_SIZE, fp_dst);
            rest_size -= BUF_SIZE;
        }

        if (rest_size > 0) {
            fread(buffer, 1, rest_size, fp_src);
            fwrite(buffer, 1, rest_size, fp_dst);
        }
        
        printf("\r%d%%", (int)((float)ftell(fp_src) / (float)input_file_size * 100));

//        printf("NAL:%02X size:%d\r\n", nal_id, nal_size);
        total_nal++;
    }
    
    printf("\r\nTotal NAL units:%ld\r\n", total_nal);

    fclose(fp_src);
    fclose(fp_dst);

    return 0;
}


Output:
1
2
3
dt2nal input output

Exited: ExitFailure 255


Create a new paste based on this one


Comments: