mirror of
https://github.com/moonlight-stream/moonlight-qt.git
synced 2026-10-11 19:13:59 +00:00
174 lines
3.8 KiB
C
174 lines
3.8 KiB
C
/*
|
|
* h264bitstream - a library for reading and writing H.264 video
|
|
* Copyright (C) 2005-2007 Auroras Entertainment, LLC
|
|
* Copyright (C) 2008-2011 Avail-TVN
|
|
*
|
|
* Written by Alex Izvorski <aizvorski@gmail.com> and Alex Giladi <alex.giladi@gmail.com>
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "h264_stream.h"
|
|
|
|
h264_stream_t* h264_new()
|
|
{
|
|
h264_stream_t* h = (h264_stream_t*)calloc(1, sizeof(h264_stream_t));
|
|
h->nal = (nal_t*)calloc(1, sizeof(nal_t));
|
|
h->sps = (sps_t*)calloc(1, sizeof(sps_t));
|
|
return h;
|
|
}
|
|
|
|
void h264_free(h264_stream_t* h)
|
|
{
|
|
free(h->nal);
|
|
free(h->sps);
|
|
free(h);
|
|
}
|
|
|
|
int find_nal_unit(uint8_t* buf, int size, int* nal_start, int* nal_end)
|
|
{
|
|
int i;
|
|
*nal_start = 0;
|
|
*nal_end = 0;
|
|
|
|
i = 0;
|
|
while (
|
|
(i + 2 < size) &&
|
|
(buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) &&
|
|
(i + 3 >= size || buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0 || buf[i+3] != 0x01)
|
|
)
|
|
{
|
|
i++;
|
|
}
|
|
|
|
if (i + 2 >= size) { return 0; }
|
|
|
|
if (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01)
|
|
{
|
|
i++;
|
|
}
|
|
|
|
if (i + 2 >= size || buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) { return 0; }
|
|
i += 3;
|
|
*nal_start = i;
|
|
|
|
while (
|
|
(i + 2 < size) &&
|
|
(buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0) &&
|
|
(buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01)
|
|
)
|
|
{
|
|
i++;
|
|
}
|
|
|
|
if (i + 2 >= size) { *nal_end = size; return (*nal_end - *nal_start); }
|
|
|
|
*nal_end = i;
|
|
return (*nal_end - *nal_start);
|
|
}
|
|
|
|
int rbsp_to_nal(const uint8_t* rbsp_buf, const int* rbsp_size, uint8_t* nal_buf, int* nal_size)
|
|
{
|
|
int i;
|
|
int j = 1;
|
|
int count = 0;
|
|
|
|
if (*nal_size > 0) { nal_buf[0] = 0x00; }
|
|
|
|
for ( i = 0; i < *rbsp_size ; )
|
|
{
|
|
if ( j >= *nal_size )
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
if ( ( count == 2 ) && !(rbsp_buf[i] & 0xFC) )
|
|
{
|
|
nal_buf[j] = 0x03;
|
|
j++;
|
|
count = 0;
|
|
continue;
|
|
}
|
|
nal_buf[j] = rbsp_buf[i];
|
|
if ( rbsp_buf[i] == 0x00 )
|
|
{
|
|
count++;
|
|
}
|
|
else
|
|
{
|
|
count = 0;
|
|
}
|
|
i++;
|
|
j++;
|
|
}
|
|
|
|
*nal_size = j;
|
|
return j;
|
|
}
|
|
|
|
int nal_to_rbsp(const uint8_t* nal_buf, int* nal_size, uint8_t* rbsp_buf, int* rbsp_size)
|
|
{
|
|
int i;
|
|
int j = 0;
|
|
int count = 0;
|
|
|
|
for( i = 0; i < *nal_size; i++ )
|
|
{
|
|
if( ( count == 2 ) && ( nal_buf[i] < 0x03) )
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
if( ( count == 2 ) && ( nal_buf[i] == 0x03) )
|
|
{
|
|
if((i < *nal_size - 1) && (nal_buf[i+1] > 0x03))
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
if(i == *nal_size - 1)
|
|
{
|
|
break;
|
|
}
|
|
|
|
i++;
|
|
count = 0;
|
|
}
|
|
|
|
if ( j >= *rbsp_size )
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
rbsp_buf[j] = nal_buf[i];
|
|
if(nal_buf[i] == 0x00)
|
|
{
|
|
count++;
|
|
}
|
|
else
|
|
{
|
|
count = 0;
|
|
}
|
|
j++;
|
|
}
|
|
|
|
*nal_size = i;
|
|
*rbsp_size = j;
|
|
return j;
|
|
}
|