This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
262 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// https://github.com/dialup-inc/ascii/blob/master/vpx/decoder.c | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#define VPX_CODEC_DISABLE_COMPAT 1 | ||
#include "vpx/vp8dx.h" | ||
#include "vpx/vpx_decoder.h" | ||
|
||
int vpx_init_dec(vpx_codec_ctx_t *ctx) { | ||
vpx_codec_iface_t *interface = vpx_codec_vp8_dx(); | ||
|
||
// Initialize codec | ||
int flags = 0; | ||
vpx_codec_err_t err = vpx_codec_dec_init(ctx, interface, NULL, flags); | ||
if (err) { | ||
return err; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int vpx_decode(vpx_codec_ctx_t *ctx, const char *frame, int frame_len, | ||
char *yv12_frame, int yv12_cap, int *yv12_len) { | ||
// Decode the frame | ||
vpx_codec_err_t err = | ||
vpx_codec_decode(ctx, (const unsigned char *)frame, frame_len, NULL, 0); | ||
if (err) { | ||
return err; | ||
} | ||
|
||
// Write decoded data to yv12_frame | ||
vpx_codec_iter_t iter = NULL; | ||
vpx_image_t *img; | ||
|
||
while ((img = vpx_codec_get_frame(ctx, &iter))) { | ||
for (int plane = 0; plane < 3; plane++) { | ||
unsigned char *buf = img->planes[plane]; | ||
for (int y = 0; y < (plane ? (img->d_h + 1) >> 1 : img->d_h); y++) { | ||
if (*yv12_len > yv12_cap) { | ||
return VPX_CODEC_MEM_ERROR; | ||
} | ||
|
||
int len = (plane ? (img->d_w + 1) >> 1 : img->d_w); | ||
memcpy(yv12_frame + *yv12_len, buf, len); | ||
buf += img->stride[plane]; | ||
*yv12_len += len; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int vpx_cleanup_dec(vpx_codec_ctx_t *ctx) { | ||
vpx_codec_err_t err = vpx_codec_destroy(ctx); | ||
if (err) { | ||
return err; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// https://github.com/dialup-inc/ascii/blob/master/vpx/decoder.go | ||
package vpx | ||
|
||
/* | ||
#cgo pkg-config: --static vpx | ||
#include "vpx/vpx_decoder.h" | ||
int vpx_init_dec(vpx_codec_ctx_t *ctx); | ||
int vpx_decode(vpx_codec_ctx_t *ctx, const char* frame, int frame_len, char* yv12_frame, int yv12_len, int *decoded_len); | ||
int vpx_cleanup_dec(vpx_codec_ctx_t *ctx); | ||
*/ | ||
import "C" | ||
import ( | ||
"sync" | ||
"unsafe" | ||
) | ||
|
||
type Decoder struct { | ||
mu sync.Mutex | ||
ctx C.vpx_codec_ctx_t | ||
} | ||
|
||
func NewDecoder() (*Decoder, error) { | ||
d := &Decoder{} | ||
ret := C.vpx_init_dec(&d.ctx) | ||
if ret != 0 { | ||
return nil, VPXCodecErr(ret) | ||
} | ||
return d, nil | ||
} | ||
|
||
func (d *Decoder) Close() error { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
|
||
ret := C.vpx_cleanup_dec(&d.ctx) | ||
if ret != 0 { | ||
return VPXCodecErr(ret) | ||
} | ||
return nil | ||
} | ||
|
||
func (d *Decoder) Decode(out, b []byte) (int, error) { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
|
||
if len(b) == 0 { | ||
return 0, nil | ||
} | ||
|
||
n, err := d.decode(out, b) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return n, nil | ||
} | ||
|
||
func (d *Decoder) decode(out, in []byte) (n int, err error) { | ||
inP := (*C.char)(unsafe.Pointer(&in[0])) | ||
inL := C.int(len(in)) | ||
|
||
outP := (*C.char)(unsafe.Pointer(&out[0])) | ||
outCap := C.int(cap(out)) | ||
outL := (*C.int)(unsafe.Pointer(&n)) | ||
|
||
ret := C.vpx_decode(&d.ctx, inP, inL, outP, outCap, outL) | ||
if ret != 0 { | ||
return n, VPXCodecErr(ret) | ||
} | ||
|
||
return n, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// https://github.com/dialup-inc/ascii/blob/master/vpx/errors.go | ||
package vpx | ||
|
||
import "fmt" | ||
|
||
type VPXCodecErr int | ||
|
||
const ( | ||
VPX_CODEC_OK VPXCodecErr = iota | ||
VPX_CODEC_ERROR | ||
VPX_CODEC_MEM_ERROR | ||
VPX_CODEC_ABI_MISMATCH | ||
VPX_CODEC_INCAPABLE | ||
VPX_CODEC_UNSUP_BITSTREAM | ||
VPX_CODEC_UNSUP_FEATURE | ||
VPX_CODEC_CORRUPT_FRAME | ||
VPX_CODEC_INVALID_PARAM | ||
VPX_CODEC_LIST_END | ||
) | ||
|
||
func (e VPXCodecErr) Error() string { | ||
switch e { | ||
case VPX_CODEC_OK: | ||
return "Success" | ||
case VPX_CODEC_ERROR: | ||
return "Unspecified internal error" | ||
case VPX_CODEC_MEM_ERROR: | ||
return "Memory allocation error" | ||
case VPX_CODEC_ABI_MISMATCH: | ||
return "ABI version mismatch" | ||
case VPX_CODEC_INCAPABLE: | ||
return "Codec does not implement requested capability" | ||
case VPX_CODEC_UNSUP_BITSTREAM: | ||
return "Bitstream not supported by this decoder" | ||
case VPX_CODEC_UNSUP_FEATURE: | ||
return "Bitstream required feature not supported by this decoder" | ||
case VPX_CODEC_CORRUPT_FRAME: | ||
return "Corrupt frame detected" | ||
case VPX_CODEC_INVALID_PARAM: | ||
return "Invalid parameter" | ||
case VPX_CODEC_LIST_END: | ||
return "End of iterated list" | ||
default: | ||
return fmt.Sprintf("codec error: %d", e) | ||
} | ||
} |