summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2013-12-17 02:39:45 +0100
committerwm4 <wm4@nowhere>2013-12-17 02:39:45 +0100
commit0112143fdaae0a6264d9e02355e9dc0ca4f7741c (patch)
treebbbe9527d1e1490e37b67d97398c5bc19c7794cd /common
parent73a5417950a2d21a397597c05521725f3d125993 (diff)
downloadmpv-0112143fdaae0a6264d9e02355e9dc0ca4f7741c.tar.bz2
mpv-0112143fdaae0a6264d9e02355e9dc0ca4f7741c.tar.xz
Split mpvcore/ into common/, misc/, bstr/
Diffstat (limited to 'common')
-rw-r--r--common/asxparser.c581
-rw-r--r--common/asxparser.h27
-rw-r--r--common/av_common.c178
-rw-r--r--common/av_common.h39
-rw-r--r--common/av_log.c176
-rw-r--r--common/av_log.h2
-rw-r--r--common/av_opts.c55
-rw-r--r--common/av_opts.h30
-rw-r--r--common/codecs.c147
-rw-r--r--common/codecs.h43
-rw-r--r--common/common.c163
-rw-r--r--common/common.h81
-rw-r--r--common/cpudetect.c56
-rw-r--r--common/cpudetect.h40
-rw-r--r--common/encode.h21
-rw-r--r--common/encode_lavc.c1115
-rw-r--r--common/encode_lavc.h101
-rw-r--r--common/global.h12
-rw-r--r--common/msg.c389
-rw-r--r--common/msg.h180
-rw-r--r--common/playlist.c241
-rw-r--r--common/playlist.h82
-rw-r--r--common/playlist_parser.c566
-rw-r--r--common/playlist_parser.h29
-rw-r--r--common/version.c26
25 files changed, 4380 insertions, 0 deletions
diff --git a/common/asxparser.c b/common/asxparser.c
new file mode 100644
index 0000000000..5b1d5652a3
--- /dev/null
+++ b/common/asxparser.c
@@ -0,0 +1,581 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "playlist.h"
+#include "playlist_parser.h"
+#include "stream/stream.h"
+#include "asxparser.h"
+#include "common/msg.h"
+
+
+typedef struct ASX_Parser_t ASX_Parser_t;
+
+typedef struct {
+ char* buffer;
+ int line;
+} ASX_LineSave_t;
+
+struct ASX_Parser_t {
+ int line; // Curent line
+ ASX_LineSave_t *ret_stack;
+ int ret_stack_size;
+ char* last_body;
+ int deep;
+ struct playlist *pl;
+};
+
+ASX_Parser_t *asx_parser_new(struct playlist *pl);
+
+void
+asx_parser_free(ASX_Parser_t* parser);
+
+/*
+ * Return -1 on error, 0 when nothing is found, 1 on sucess
+ */
+int
+asx_get_element(ASX_Parser_t* parser,char** _buffer,
+ char** _element,char** _body,char*** _attribs);
+
+int
+asx_parse_attribs(ASX_Parser_t* parser,char* buffer,char*** _attribs);
+
+/////// Attribs utils
+
+char*
+asx_get_attrib(const char* attrib,char** attribs);
+
+#define asx_free_attribs(a) asx_list_free(&a,free)
+
+////// List utils
+
+typedef void (*ASX_FreeFunc)(void* arg);
+
+void
+asx_list_free(void* list_ptr,ASX_FreeFunc free_func);
+
+
+////// List utils
+
+void
+asx_list_free(void* list_ptr,ASX_FreeFunc free_func) {
+ void** ptr = *(void***)list_ptr;
+ if(ptr == NULL) return;
+ if(free_func != NULL) {
+ for( ; *ptr != NULL ; ptr++)
+ free_func(*ptr);
+ }
+ free(*(void**)list_ptr);
+ *(void**)list_ptr = NULL;
+}
+
+/////// Attribs utils
+
+char*
+asx_get_attrib(const char* attrib,char** attribs) {
+ char** ptr;
+
+ if(attrib == NULL || attribs == NULL) return NULL;
+ for(ptr = attribs; ptr[0] != NULL; ptr += 2){
+ if(strcasecmp(ptr[0],attrib) == 0)
+ return strdup(ptr[1]);
+ }
+ return NULL;
+}
+
+#define asx_warning_attrib_required(p,e,a) mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : element %s don't have the required attribute %s",p->line,e,a)
+#define asx_warning_body_parse_error(p,e) mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : error while parsing %s body",p->line,e)
+
+ASX_Parser_t *asx_parser_new(struct playlist *pl)
+{
+ ASX_Parser_t* parser = calloc(1,sizeof(ASX_Parser_t));
+ parser->pl = pl;
+ return parser;
+}
+
+void
+asx_parser_free(ASX_Parser_t* parser) {
+ if(!parser) return;
+ free(parser->ret_stack);
+ free(parser);
+
+}
+
+#define LETTER "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+#define SPACE " \n\t\r"
+
+int
+asx_parse_attribs(ASX_Parser_t* parser,char* buffer,char*** _attribs) {
+ char *ptr1, *ptr2, *ptr3;
+ int n_attrib = 0;
+ char **attribs = NULL;
+ char *attrib, *val;
+
+ ptr1 = buffer;
+ while(1) {
+ for( ; strchr(SPACE,*ptr1) != NULL; ptr1++) { // Skip space
+ if(*ptr1 == '\0') break;
+ }
+ ptr3 = strchr(ptr1,'=');
+ if(ptr3 == NULL) break;
+ for(ptr2 = ptr3-1; strchr(SPACE,*ptr2) != NULL; ptr2--) {
+ if (ptr2 == ptr1) {
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : this should never append, back to attribute begin while skipping end space",parser->line);
+ break;
+ }
+ }
+ attrib = malloc(ptr2-ptr1+2);
+ strncpy(attrib,ptr1,ptr2-ptr1+1);
+ attrib[ptr2-ptr1+1] = '\0';
+
+ ptr1 = strchr(ptr3,'"');
+ if(ptr1 == NULL || ptr1[1] == '\0') ptr1 = strchr(ptr3,'\'');
+ if(ptr1 == NULL || ptr1[1] == '\0') {
+ mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : can't find attribute %s value",parser->line,attrib);
+ free(attrib);
+ break;
+ }
+ ptr2 = strchr(ptr1+1,ptr1[0]);
+ if (ptr2 == NULL) {
+ mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : value of attribute %s isn't finished",parser->line,attrib);
+ free(attrib);
+ break;
+ }
+ ptr1++;
+ val = malloc(ptr2-ptr1+1);
+ strncpy(val,ptr1,ptr2-ptr1);
+ val[ptr2-ptr1] = '\0';
+ n_attrib++;
+
+ attribs = realloc(attribs, (2 * n_attrib + 1) * sizeof(char*));
+ attribs[n_attrib*2-2] = attrib;
+ attribs[n_attrib*2-1] = val;
+
+ ptr1 = ptr2+1;
+ }
+
+ if(n_attrib > 0)
+ attribs[n_attrib*2] = NULL;
+
+ *_attribs = attribs;
+
+ return n_attrib;
+}
+
+/*
+ * Return -1 on error, 0 when nothing is found, 1 on sucess
+ */
+int
+asx_get_element(ASX_Parser_t* parser,char** _buffer,
+ char** _element,char** _body,char*** _attribs) {
+ char *ptr1,*ptr2, *ptr3, *ptr4;
+ char *attribs = NULL;
+ char *element = NULL, *body = NULL, *ret = NULL, *buffer;
+ int n_attrib = 0;
+ int body_line = 0,attrib_line,ret_line,in = 0;
+ int quotes = 0;
+
+ if(_buffer == NULL || _element == NULL || _body == NULL || _attribs == NULL) {
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : asx_get_element called with invalid value",parser->line);
+ return -1;
+ }
+
+ *_body = *_element = NULL;
+ *_attribs = NULL;
+ buffer = *_buffer;
+
+ if(buffer == NULL) return 0;
+
+ if(parser->ret_stack && /*parser->last_body && */buffer != parser->last_body) {
+ ASX_LineSave_t* ls = parser->ret_stack;
+ int i;
+ for(i = 0 ; i < parser->ret_stack_size ; i++) {
+ if(buffer == ls[i].buffer) {
+ parser->line = ls[i].line;
+ break;
+ }
+
+ }
+ if( i < parser->ret_stack_size) {
+ i++;
+ if( i < parser->ret_stack_size)
+ memmove(parser->ret_stack,parser->ret_stack+i, (parser->ret_stack_size - i)*sizeof(ASX_LineSave_t));
+ parser->ret_stack_size -= i;
+ if(parser->ret_stack_size > 0)
+ parser->ret_stack = realloc(parser->ret_stack,parser->ret_stack_size*sizeof(ASX_LineSave_t));
+ else {
+ free(parser->ret_stack);
+ parser->ret_stack = NULL;
+ }
+ }
+ }
+
+ ptr1 = buffer;
+ while(1) {
+ for( ; ptr1[0] != '<' ; ptr1++) {
+ if(ptr1[0] == '\0') {
+ ptr1 = NULL;
+ break;
+ }
+ if(ptr1[0] == '\n') parser->line++;
+ }
+ //ptr1 = strchr(ptr1,'<');
+ if(!ptr1 || ptr1[1] == '\0') return 0; // Nothing found
+
+ if(strncmp(ptr1,"<!--",4) == 0) { // Comments
+ for( ; strncmp(ptr1,"-->",3) != 0 ; ptr1++) {
+ if(ptr1[0] == '\0') {
+ ptr1 = NULL;
+ break;
+ }
+ if(ptr1[0] == '\n') parser->line++;
+ }
+ //ptr1 = strstr(ptr1,"-->");
+ if(!ptr1) {
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : unfinished comment",parser->line);
+ return -1;
+ }
+ } else {
+ break;
+ }
+ }
+
+ // Is this space skip very useful ??
+ for(ptr1++; strchr(SPACE,ptr1[0]) != NULL; ptr1++) { // Skip space
+ if(ptr1[0] == '\0') {
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
+ return -1;
+ }
+ if(ptr1[0] == '\n') parser->line++;
+ }
+
+ for(ptr2 = ptr1; strchr(LETTER,*ptr2) != NULL;ptr2++) { // Go to end of name
+ if(*ptr2 == '\0'){
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
+ return -1;
+ }
+ if(ptr2[0] == '\n') parser->line++;
+ }
+
+ element = malloc(ptr2-ptr1+1);
+ strncpy(element,ptr1,ptr2-ptr1);
+ element[ptr2-ptr1] = '\0';
+
+ for( ; strchr(SPACE,*ptr2) != NULL; ptr2++) { // Skip space
+ if(ptr2[0] == '\0') {
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
+ free(element);
+ return -1;
+ }
+ if(ptr2[0] == '\n') parser->line++;
+ }
+ attrib_line = parser->line;
+
+
+
+ for(ptr3 = ptr2; ptr3[0] != '\0'; ptr3++) { // Go to element end
+ if(ptr3[0] == '"') quotes ^= 1;
+ if(!quotes && (ptr3[0] == '>' || strncmp(ptr3,"/>",2) == 0))
+ break;
+ if(ptr3[0] == '\n') parser->line++;
+ }
+ if(ptr3[0] == '\0' || ptr3[1] == '\0') { // End of file
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
+ free(element);
+ return -1;
+ }
+
+ // Save attribs string
+ if(ptr3-ptr2 > 0) {
+ attribs = malloc(ptr3-ptr2+1);
+ strncpy(attribs,ptr2,ptr3-ptr2);
+ attribs[ptr3-ptr2] = '\0';
+ }
+ //bs_line = parser->line;
+ if(ptr3[0] != '/') { // Not Self closed element
+ ptr3++;
+ for( ; strchr(SPACE,*ptr3) != NULL; ptr3++) { // Skip space on body begin
+ if(*ptr3 == '\0') {
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing %s element body",parser->line,element);
+ free(element);
+ free(attribs);
+ return -1;
+ }
+ if(ptr3[0] == '\n') parser->line++;
+ }
+ ptr4 = ptr3;
+ body_line = parser->line;
+ while(1) { // Find closing element
+ for( ; ptr4[0] != '<' ; ptr4++) {
+ if(ptr4[0] == '\0') {
+ ptr4 = NULL;
+ break;
+ }
+ if(ptr4[0] == '\n') parser->line++;
+ }
+ if(ptr4 && strncmp(ptr4,"<!--",4) == 0) { // Comments
+ for( ; strncmp(ptr4,"-->",3) != 0 ; ptr4++) {
+ if(ptr4[0] == '\0') {
+ ptr4 = NULL;
+ break;
+ }
+ if(ptr1[0] == '\n') parser->line++;
+ }
+ continue;
+ }
+ if(ptr4 == NULL || ptr4[1] == '\0') {
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing %s element body",parser->line,element);
+ free(element);
+ free(attribs);
+ return -1;
+ }
+ if(ptr4[1] != '/' && strncasecmp(element,ptr4+1,strlen(element)) == 0) {
+ in++;
+ ptr4+=2;
+ continue;
+ } else if(strncasecmp(element,ptr4+2,strlen(element)) == 0) { // Extract body
+ if(in > 0) {
+ in--;
+ ptr4 += 2+strlen(element);
+ continue;
+ }
+ ret = ptr4+strlen(element)+3;
+ if(ptr4 != ptr3) {
+ ptr4--;
+ for( ; ptr4 != ptr3 && strchr(SPACE,*ptr4) != NULL; ptr4--) ;// Skip space on body end
+ // if(ptr4[0] == '\0') parser->line--;
+ //}
+ ptr4++;
+ body = malloc(ptr4-ptr3+1);
+ strncpy(body,ptr3,ptr4-ptr3);
+ body[ptr4-ptr3] = '\0';
+ }
+ break;
+ } else {
+ ptr4 += 2;
+ }
+ }
+ } else {
+ ret = ptr3 + 2; // 2 is for />
+ }
+
+ for( ; ret[0] != '\0' && strchr(SPACE,ret[0]) != NULL; ret++) { // Skip space
+ if(ret[0] == '\n') parser->line++;
+ }
+
+ ret_line = parser->line;
+
+ if(attribs) {
+ parser->line = attrib_line;
+ n_attrib = asx_parse_attribs(parser,attribs,_attribs);
+ free(attribs);
+ if(n_attrib < 0) {
+ mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : error while parsing element %s attributes",parser->line,element);
+ free(element);
+ free(body);
+ return -1;
+ }
+ } else
+ *_attribs = NULL;
+
+ *_element = element;
+ *_body = body;
+
+ parser->last_body = body;
+ parser->ret_stack_size++;
+ parser->ret_stack = realloc(parser->ret_stack,parser->ret_stack_size*sizeof(ASX_LineSave_t));
+ if(parser->ret_stack_size > 1)
+ memmove(parser->ret_stack+1,parser->ret_stack,(parser->ret_stack_size-1)*sizeof(ASX_LineSave_t));
+ parser->ret_stack[0].buffer = ret;
+ parser->ret_stack[0].line = ret_line;
+ parser->line = body ? body_line : ret_line;
+
+ *_buffer = ret;
+ return 1;
+
+}
+
+static void
+asx_parse_ref(ASX_Parser_t* parser, char** attribs) {
+ char *href;
+
+ href = asx_get_attrib("HREF",attribs);
+ if(href == NULL) {
+ asx_warning_attrib_required(parser,"REF" ,"HREF" );
+ return;
+ }
+#if 0
+ // replace http my mmshttp to avoid infinite loops
+ // disabled since some playlists for e.g. WinAMP use asx as well
+ // "-user-agent NSPlayer/4.1.0.3856" is a possible workaround
+ if (strncmp(href, "http://", 7) == 0) {
+ char *newref = malloc(3 + strlen(href) + 1);
+ strcpy(newref, "mms");
+ strcpy(newref + 3, href);
+ free(href);
+ href = newref;
+ }
+#endif
+
+ playlist_add_file(parser->pl, href);
+
+ mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding file %s to element entry\n",href);
+
+ free(href);
+
+}
+
+static void asx_parse_entryref(ASX_Parser_t* parser,char* buffer,char** _attribs) {
+ char *href;
+
+ if(parser->deep > 0)
+ return;
+
+ href = asx_get_attrib("HREF",_attribs);
+ if(href == NULL) {
+ asx_warning_attrib_required(parser,"ENTRYREF" ,"HREF" );
+ return;
+ }
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Recursive playlist %s\n", href);
+ playlist_add_file(parser->pl, href);
+ free(href);
+ //mp_msg(MSGT_PLAYTREE,MSGL_INFO,"Need to implement entryref\n");
+}
+
+static void asx_parse_entry(ASX_Parser_t* parser,char* buffer,char** _attribs) {
+ char *element,*body,**attribs;
+ int r;
+
+ while(buffer && buffer[0] != '\0') {
+ r = asx_get_element(parser,&buffer,&element,&body,&attribs);
+ if(r < 0) {
+ asx_warning_body_parse_error(parser,"ENTRY");
+ return;
+ } else if (r == 0) { // No more element
+ break;
+ }
+ if(strcasecmp(element,"REF") == 0) {
+ asx_parse_ref(parser,attribs);
+ mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to entry\n",element);
+ } else
+ mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Ignoring element %s\n",element);
+ free(body);
+ asx_free_attribs(attribs);
+ }
+
+}
+
+
+static void asx_parse_repeat(ASX_Parser_t* parser,char* buffer,char** _attribs) {
+ char *element,*body,**attribs;
+ int r;
+
+ asx_get_attrib("COUNT",_attribs);
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Ignoring repeated playlist entries\n");
+
+ while(buffer && buffer[0] != '\0') {
+ r = asx_get_element(parser,&buffer,&element,&body,&attribs);
+ if(r < 0) {
+ asx_warning_body_parse_error(parser,"REPEAT");
+ return;
+ } else if (r == 0) { // No more element
+ break;
+ }
+ if(strcasecmp(element,"ENTRY") == 0) {
+ asx_parse_entry(parser,body,attribs);
+ } else if(strcasecmp(element,"ENTRYREF") == 0) {
+ asx_parse_entryref(parser,body,attribs);
+ } else if(strcasecmp(element,"REPEAT") == 0) {
+ asx_parse_repeat(parser,body,attribs);
+ } else
+ mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Ignoring element %s\n",element);
+ free(body);
+ asx_free_attribs(attribs);
+ }
+
+}
+
+
+bool asx_parse(char* buffer, struct playlist *pl)
+{
+ char *element,*asx_body,**asx_attribs,*body = NULL, **attribs;
+ int r;
+ ASX_Parser_t* parser = asx_parser_new(pl);
+
+ parser->line = 1;
+ parser->deep = 0;
+
+ r = asx_get_element(parser,&buffer,&element,&asx_body,&asx_attribs);
+ if(r < 0) {
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : Syntax error ???",parser->line);
+ asx_parser_free(parser);
+ return false;
+ } else if(r == 0) { // No contents
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"empty asx element");
+ asx_parser_free(parser);
+ return false;
+ }
+
+ if(strcasecmp(element,"ASX") != 0) {
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"first element isn't ASX, it's %s\n",element);
+ asx_free_attribs(asx_attribs);
+ asx_parser_free(parser);
+ return false;
+ }
+
+ if(!asx_body) {
+ mp_msg(MSGT_PLAYTREE,MSGL_ERR,"ASX element is empty");
+ asx_free_attribs(asx_attribs);
+ asx_parser_free(parser);
+ return false;
+ }
+
+ buffer = asx_body;
+ while(buffer && buffer[0] != '\0') {
+ r = asx_get_element(parser,&buffer,&element,&body,&attribs);
+ if(r < 0) {
+ asx_warning_body_parse_error(parser,"ASX");
+ asx_parser_free(parser);
+ return false;
+ } else if (r == 0) { // No more element
+ break;
+ }
+ if(strcasecmp(element,"ENTRY") == 0) {
+ asx_parse_entry(parser,body,attribs);
+ } else if(strcasecmp(element,"ENTRYREF") == 0) {
+ asx_parse_entryref(parser,body,attribs);
+ } else if(strcasecmp(element,"REPEAT") == 0) {
+ asx_parse_repeat(parser,body,attribs);
+ } else
+ mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Ignoring element %s\n",element);
+ free(body);
+ asx_free_attribs(attribs);
+ }
+
+ free(asx_body);
+ asx_free_attribs(asx_attribs);
+ asx_parser_free(parser);
+ return true;
+}
diff --git a/common/asxparser.h b/common/asxparser.h
new file mode 100644
index 0000000000..e49a2cedc0
--- /dev/null
+++ b/common/asxparser.h
@@ -0,0 +1,27 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPLAYER_ASXPARSER_H
+#define MPLAYER_ASXPARSER_H
+
+#include <stdbool.h>
+
+struct playlist;
+bool asx_parse(char* buffer, struct playlist *pl);
+
+#endif /* MPLAYER_ASXPARSER_H */
diff --git a/common/av_common.c b/common/av_common.c
new file mode 100644
index 0000000000..cccb0f755e
--- /dev/null
+++ b/common/av_common.c
@@ -0,0 +1,178 @@
+/*
+ * This file is part of mpv.
+ *
+ * mpv is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * mpv 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with mpv. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <assert.h>
+
+#include <libavutil/common.h>
+#include <libavcodec/avcodec.h>
+
+#include "common/common.h"
+#include "common/msg.h"
+#include "demux/packet.h"
+#include "av_common.h"
+#include "codecs.h"
+
+#include "osdep/numcores.h"
+
+
+// Copy the codec-related fields from st into avctx. This does not set the
+// codec itself, only codec related header data provided by libavformat.
+// The goal is to initialize a new decoder with the header data provided by
+// libavformat, and unlike avcodec_copy_context(), allow the user to create
+// a clean AVCodecContext for a manually selected AVCodec.
+// This is strictly for decoding only.
+void mp_copy_lav_codec_headers(AVCodecContext *avctx, AVCodecContext *st)
+{
+ if (st->extradata_size) {
+ av_free(avctx->extradata);
+ avctx->extradata_size = 0;
+ avctx->extradata =
+ av_mallocz(st->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
+ if (avctx->extradata) {
+ avctx->extradata_size = st->extradata_size;
+ memcpy(avctx->extradata, st->extradata, st->extradata_size);
+ }
+ }
+ avctx->codec_tag = st->codec_tag;
+ avctx->stream_codec_tag = st->stream_codec_tag;
+ avctx->bit_rate = st->bit_rate;
+ avctx->width = st->width;
+ avctx->height = st->height;
+ avctx->pix_fmt = st->pix_fmt;
+ avctx->sample_aspect_ratio = st->sample_aspect_ratio;
+ avctx->chroma_sample_location = st->chroma_sample_location;
+ avctx->sample_rate = st->sample_rate;
+ avctx->channels = st->channels;
+ avctx->block_align = st->block_align;
+ avctx->channel_layout = st->channel_layout;
+ avctx->audio_service_type = st->audio_service_type;
+ avctx->bits_per_coded_sample = st->bits_per_coded_sample;
+}
+
+// We merely pass-through our PTS/DTS as an int64_t; libavcodec won't use it.
+union pts { int64_t i; double d; };
+
+// Convert the mpv style timestamp (seconds as double) to a libavcodec style
+// timestamp (integer units in a given timebase).
+//
+// If the given timebase is NULL or invalid, pass through the mpv timestamp by
+// reinterpret casting them to int64_t. In this case, the timestamps will be
+// non-sense for libavcodec, but we expect that it doesn't interpret them,
+// and treats them as opaque.
+int64_t mp_pts_to_av(double mp_pts, AVRational *tb)
+{
+ assert(sizeof(int64_t) >= sizeof(double));
+ if (tb && tb->num > 0 && tb->den > 0)
+ return mp_pts == MP_NOPTS_VALUE ? AV_NOPTS_VALUE : mp_pts / av_q2d(*tb);
+ // The + 0.0 is to squash possible negative zero mp_pts, which would
+ // happen to end up as AV_NOPTS_VALUE.
+ return (union pts){.d = mp_pts + 0.0}.i;
+}
+
+// Inverse of mp_pts_to_av(). (The timebases must be exactly the same.)
+double mp_pts_from_av(int64_t av_pts, AVRational *tb)
+{
+ assert(sizeof(int64_t) >= sizeof(double));
+ if (tb && tb->num > 0 && tb->den > 0)
+ return av_pts == AV_NOPTS_VALUE ? MP_NOPTS_VALUE : av_pts * av_q2d(*tb);
+ // Should libavcodec set the PTS to AV_NOPTS_VALUE, it would end up as
+ // non-sense (usually negative zero) when unwrapped to double.
+ return av_pts == AV_NOPTS_VALUE ? MP_NOPTS_VALUE : (union pts){.i = av_pts}.d;
+}
+
+// Set dst from mpkt. Note that dst is not refcountable.
+// mpkt can be NULL to generate empty packets (used to flush delayed data).
+// Sets pts/dts using mp_pts_to_av(ts, tb). (Be aware of the implications.)
+// Set duration field only if tb is set.
+void mp_set_av_packet(AVPacket *dst, struct demux_packet *mpkt, AVRational *tb)
+{
+ av_init_packet(dst);
+ dst->data = mpkt ? mpkt->buffer : NULL;
+ dst->size = mpkt ? mpkt->len : 0;
+ /* Some codecs (ZeroCodec, some cases of PNG) may want keyframe info
+ * from demuxer. */
+ if (mpkt && mpkt->keyframe)
+ dst->flags |= AV_PKT_FLAG_KEY;
+ if (mpkt && mpkt->avpacket) {
+ dst->side_data = mpkt->avpacket->side_data;
+ dst->side_data_elems = mpkt->avpacket->side_data_elems;
+ }
+ if (mpkt && tb && tb->num > 0 && tb->den > 0)
+ dst->duration = mpkt->duration / av_q2d(*tb);
+ dst->pts = mp_pts_to_av(mpkt ? mpkt->pts : MP_NOPTS_VALUE, tb);
+ dst->dts = mp_pts_to_av(mpkt ? mpkt->dts : MP_NOPTS_VALUE, tb);
+}
+
+void mp_set_avcodec_threads(AVCodecContext *avctx, int threads)
+{
+ if (threads == 0) {
+ threads = default_thread_count();
+ if (threads < 1) {
+ mp_msg(MSGT_GLOBAL, MSGL_WARN, "Could not determine "
+ "thread count to use, defaulting to 1.\n");
+ threads = 1;
+ }
+ // Apparently some libavcodec versions have or had trouble with more
+ // than 16 threads, and/or print a warning when using > 16.
+ threads = MPMIN(threads, 16);
+ }
+ avctx->thread_count = threads;
+}
+
+void mp_add_lavc_decoders(struct mp_decoder_list *list, enum AVMediaType type)
+{
+ AVCodec *cur = NULL;
+ for (;;) {
+ cur = av_codec_next(cur);
+ if (!cur)
+ break;
+ if (av_codec_is_decoder(cur) && cur->type == type) {
+ mp_add_decoder(list, "lavc", mp_codec_from_av_codec_id(cur->id),
+ cur->name, cur->long_name);
+ }
+ }
+}
+
+int mp_codec_to_av_codec_id(const char *codec)
+{
+ int id = AV_CODEC_ID_NONE;
+ if (codec) {
+ const AVCodecDescriptor *desc = avcodec_descriptor_get_by_name(codec);
+ if (desc)
+ id = desc->id;
+ if (id == AV_CODEC_ID_NONE) {
+ AVCodec *avcodec = avcodec_find_decoder_by_name(codec);
+ if (avcodec)
+ id = avcodec->id;
+ }
+ }
+ return id;
+}
+
+const char *mp_codec_from_av_codec_id(int codec_id)
+{
+ const char *name = NULL;
+ const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
+ if (desc)
+ name = desc->name;
+ if (!name) {
+ AVCodec *avcodec = avcodec_find_decoder(codec_id);
+ if (avcodec)
+ name = avcodec->name;
+ }
+ return name;
+}
diff --git a/common/av_common.h b/common/av_common.h
new file mode 100644
index 0000000000..7bf2d64d9e
--- /dev/null
+++ b/common/av_common.h
@@ -0,0 +1,39 @@
+/*
+ * This file is part of mpv.
+ *
+ * mpv is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * mpv 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with mpv. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MP_AVCOMMON_H
+#define MP_AVCOMMON_H
+
+#include <inttypes.h>
+
+#include <libavutil/avutil.h>
+#include <libavutil/rational.h>
+#include <libavcodec/avcodec.h>
+
+struct mp_decoder_list;
+struct demux_packet;
+
+void mp_copy_lav_codec_headers(AVCodecContext *avctx, AVCodecContext *st);
+void mp_set_av_packet(AVPacket *dst, struct demux_packet *mpkt, AVRational *tb);
+int64_t mp_pts_to_av(double mp_pts, AVRational *tb);
+double mp_pts_from_av(int64_t av_pts, AVRational *tb);
+void mp_set_avcodec_threads(AVCodecContext *avctx, int threads);
+void mp_add_lavc_decoders(struct mp_decoder_list *list, enum AVMediaType type);
+int mp_codec_to_av_codec_id(const char *codec);
+const char *mp_codec_from_av_codec_id(int codec_id);
+
+#endif
diff --git a/common/av_log.c b/common/av_log.c
new file mode 100644
index 0000000000..ba47500572
--- /dev/null
+++ b/common/av_log.c
@@ -0,0 +1,176 @@
+/*
+ * av_log to mp_msg converter
+ * Copyright (C) 2006 Michael Niedermayer
+ * Copyright (C) 2009 Uoti Urpala
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more detail